I have a very simple class for the deserialisation of a JSON string into a dictionary
private static Dictionary<string,string> DeserializeJSON(string json)
{
var dict = new Dictionary<string,string>();
if (string.IsNullOrEmpty(json))
return dict;
var jsDict = JsonConvert.DeserializeObject<Dictionary<string,string>>(json);
if (jsDict != null)
dict = jsDict;
return dict;
}
This does deserialise the string, but dies when the string contains a null value for example
["ImplementID",null,"ImplementType","Flexicoil Airseeder","SeedUnits","Kg/H","SeedApplicationRate","75","CropType","Wheat(Morombi)","Fertiliser0ID","2edd3043","FertiliserRate0","100","FertiliserRateUnit0","Kg/H"]
with the following throwback
2014-04-23 01:39:07.212 myapp[63857:70b] Ouch, something went pop : Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.- at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.Object existingValue, System.String id) [0x00000] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, Boolean checkAdditionalContent) [0x00000] in :0
Is there a simple way to ignore the null value (and key) when deserializing the JSON string?