2017-04-14 82 views
-1

我有我使用Newtonsoft.Json成Dictionary<string, Dictionary<string, string>>解析嵌套的字典,LINQ

我想使用LINQ但是我奋力与嵌套部分要做到这一点把它解析为Dictionary<string, Dictionary<double, int>>反序列化JSON对象。

对于未嵌套的字典我只是用.ToDictionary(k => double.Parse(k.Key), k => int.Parse(k.Value))

感谢

+0

你是什么意思的JSON对象? JObject?或者只是一个字符串?你可以请张贴你到目前为止,所以我们可以尝试吗? –

+0

类似于:.ToDictionary(k => double.Parse(k.Key),l => l.ToDictionary(int.Parse(l.Value))) – jdweng

回答

1
 var input = new Dictionary<string, Dictionary<string, string>>(); 
     input.Add("test1", new Dictionary<string, string>()); 
     input["test1"].Add("1.2", "3"); 

     var output = input.ToDictionary(
      x => x.Key, 
      x => x.Value.ToDictionary(
       y => double.Parse(y.Key), 
       y => int.Parse(y.Value) 
      ) 
     ); 

应该做的伎俩。