0

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?

0

1 Answer 1

1

It has nothing to do with a null value. It is because the input represents a JSON Array (a sequence of values) and cannot be directly mapped to a Dictionary (a set of key-value pairs) as so.

Try it with a non-null value and you'll get the same result.

Cannot deserialize the current JSON array (e.g. [1,2,3]) .. because the [Dictionary] type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

Arguably the correct solution is to get and parse JSON Object input. Compare the posted JSON with this variation, which would have deserialized fine, even with a null value.

{"ImplementID": null, "ImplementType": "Flexicoil Airseeder", "SeedUnits": "Kg/H"}

However, if you cannot get the JSON in a sensible form, simply derserialize the JSON Object as a List and then convert the List to a Dictionary as required.

var data = JsonConvert.DeserializeObject<List<string>>(json);
var dict = new Dictionary<string,string>();
for (var i = 0; i < data.length; i += 2) {
   var key = data[i];
   var value = data[i+1];
   dict[key] = value;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.