将Json转换为Dictionary C#

时间:2016-07-14 00:24:27

标签: c# json dictionary

我想在Dictionary中将这个json转换成一个循环

查看我的代码:

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"]);

1 个答案:

答案 0 :(得分:5)

您需要将dictionaryLevelOne["note"]作为字符串传递。

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