user969153
user969153

Reputation: 566

Deserializing complex json object with dictionary

I'm using Newtonsoft Json.Net to deserialize json feed into object:

Json:

staticKey1: value,
staticKey2: value,

results: [
{
   key1: value1,
   key2: value2,
   key3: value3,
...
   keyN: valueN
}
],

C# class:

public class MyClassName
{
    public string staticKey1 { get; set; }
    public string staticKey2 { get; set; }
    public Dictionary<String, String> results { get; set; }
}

I'm using Newtonsoft.Json.JsonConvert.DeserializeObject(), but I'm getting exception:

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.

Upvotes: 0

Views: 925

Answers (1)

user969153
user969153

Reputation: 566

Actually its easy, use:

public class MyClass
{
    public string staticKey1 { get; set; }
    public string staticKey2 { get; set; }
    public IEnumerable<IDictionary<string, string>> results { get; set; }
}

But maybe there's better solution.

Upvotes: 1

Related Questions