Shachaf.Gortler Shachaf.Gortler - 1 year ago 111
JSON Question

error deserialization dynamic json

I have the following Json , which has a dynamic set of values

[ {
"Type": "Animal" }, {
"Profession": "Dog" } ]

I want to read it into an object


this works :

var objectList = JsonConvert.DeserializeObject<List<dynamic>>(rawStringJsonData);

but when I try

var objectList = JsonConvert.DeserializeObject<List<List<KeyValuePair<String,String>>>(rawStringJsonData);

I get an error

Additional information: Cannot deserialize the current JSON object
(e.g. {"name":"value"}) into type

Answer Source

You can read it as a List<Dictionary<string,string>> and then cast it to List<List<KeyValuePair<String,String>>>

Try this:

var rawStringJsonData = "[ { \"Type\": \"Animal\" }, { \"Profession\": \"Dog\" } ]";
var dictList = JsonConvert.DeserializeObject<List<Dictionary<string,string>>>(rawStringJsonData);

List<List<KeyValuePair<string,string>>> objectList =
     dictList.Select(i => i.ToList()).ToList();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download