Shachaf.Gortler Shachaf.Gortler - 3 months ago 18
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

List<List<KeyValuePair<String,String>>>


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

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();
Comments