Wallace Ferreira Wallace Ferreira - 4 months ago 29
JSON Question

Convert Json to Dictionary C#

I want transform this json in Dictionary for make one loop

Look my code:

string json = @"{""name"":""Joe"",""note"":{""a1"":10,""a2"":9,""a3"":5}}";

Dictionary<string, object> dictionaryLevelOne = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
Dictionary<string, object> dictionaryLevelTwo = JsonConvert.DeserializeObject<Dictionary<string, object>>(dictionaryLevelOne ["note"]);

Answer

You need to pass dictionaryLevelOne["note"] as a string.

string json = @"{""name"":""Joe"",""note"":{""a1"":10,""a2"":9,""a3"":5}}";

Dictionary<string, object> dictionaryLevelOne = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
Dictionary<string, object> dictionaryLevelTwo = JsonConvert.DeserializeObject<Dictionary<string, object>>(dictionaryLevelOne["note"].ToString());