Reputation: 753
I need to deserialize simple json to Dictionary or something similar
json:
{"answer":"ok","hash":"hash123","name":"name123","kills":0,"online":0,"currentTime":"2018-01-06 21:18:30","endDate":"2018-01-06 21:18:30","configUrl":"configUrl123","extend":[]}
I have a problem with "extend" array at this json object. It gives me error as my Dictionary is wrong for array. If i delete it all works fine, but i need to save it.
var json = JsonConvert.DeserializeObject<Dictionary<string, string>>(data);
Console.WriteLine(json["answer"]);
Sure i can fix this problem with class, but i can't use it.
Upvotes: 1
Views: 1391
Reputation: 13146
I suggest you to deserialize it with a known object;
public class JsonObject
{
public string answer { get; set; }
public string hash { get; set; }
public string name { get; set; }
public int kills { get; set; }
public int online { get; set; }
public string currentTime { get; set; }
public string endDate { get; set; }
public string configUrl { get; set; }
public List<object> extend { get; set; }
}
var json = JsonConvert.DeserializeObject<JsonObject>(jsonStr);
Console.WriteLine(json.answer);
If you want to stay with dictionary you should deserialize like Dictionary<string, object>
, because extend
key not contains string
;
var jsonDictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonStr);
Console.WriteLine(jsonDictionary["answer"]);
Upvotes: 2
Reputation: 9425
You are trying to deserialize an array into a string, so that's not going to work. Try using DeserializeObject<Dictionary<string, object>>
instead.
Upvotes: 2