如何遍历嵌套的Dictionary ?

时间:2023-01-23 17:20:27

I have a JSON file that I need to deserialize using the class System.Web.Script.Serialization. The following code:

我有一个JSON文件,我需要使用类System.Web.Script.Serialization反序列化。以下代码:

string json = File.ReadAllText(@"C:\file.json");
JavaScriptSerializer ser = new JavaScriptSerializer();
var dict = ser.Deserialize<Dictionary<string, object>>(json);

creates a nested dictionary, something like this:

创建一个嵌套字典,如下所示:

Dictionary<string, Object> outerDictionary;
Dictionary<string, Object> middleDictionary;
Dictionary<string, string> innerDictionary;

I now need to grab some of the values from the innerDictionary into a C# object class, like:

我现在需要从innerDictionary中获取一些值到C#对象类中,如:

public class Location
{
   public string Id { get; set; }
   public string Name { get; set; }
   public string Street { get; set; }
   public string City { get; set; }
   public string State { get; set; }
   public string Zip { get; set; }
}

So my question is, how can I iterate over the nested disctionary to get the values I need at the innermost level?

所以我的问题是,我如何迭代嵌套的disctionary以获得我最需要的值?

UPDATE

With the help of Jonesopolis I now have the correct loop structure, but I am still unable to get the values from the innermost dictionary?

在Jonesopolis的帮助下,我现在拥有正确的循环结构,但我仍然无法从最里面的字典中获取值?

foreach (var outer in dict)
{
    foreach (var middle in (Dictionary<string, object>)outer.Value)
    {
        foreach (var inner in (ArrayList)middle.Value)
        {
            foreach (var loc in (Dictionary<string, object>)inner)
            {
            }
         }
    }
}

2 个解决方案

#1


1  

If you don't like the nested foreach in Jonesopolis's answer, then you can use LINQ this way:

如果您不喜欢Jonesopolis答案中的嵌套foreach,那么您可以这样使用LINQ:

var data = from outer in dict
           from middle in (Dictionary<string, object>) outer.Value
           from inner in (Dictionary<string, string>) middle.Value
           select new { outer, middle, inner };

foreach (var item in data) {
    // do things with item.inner
}

#2


0  

if you really want to work with those objects:

如果你真的想使用这些对象:

foreach(var outer in dict)
{
    foreach (var middle in (Dictionary<string, object>) outer.Value)
    {
         foreach (var inner in (Dictionary<string, string>) middle.Value)
         {
              var location = new Location();
              location.Id = inner["Id"]; 
              //etc..
         }      
    }
}

though, it would be much better to create a class structure that you can deserialize your json to, that accurately represents your data, instead of casting Dictionaries.

但是,创建一个可以反序列化json的类结构会更好,它可以准确地表示您的数据,而不是转换Dictionaries。

#1


1  

If you don't like the nested foreach in Jonesopolis's answer, then you can use LINQ this way:

如果您不喜欢Jonesopolis答案中的嵌套foreach,那么您可以这样使用LINQ:

var data = from outer in dict
           from middle in (Dictionary<string, object>) outer.Value
           from inner in (Dictionary<string, string>) middle.Value
           select new { outer, middle, inner };

foreach (var item in data) {
    // do things with item.inner
}

#2


0  

if you really want to work with those objects:

如果你真的想使用这些对象:

foreach(var outer in dict)
{
    foreach (var middle in (Dictionary<string, object>) outer.Value)
    {
         foreach (var inner in (Dictionary<string, string>) middle.Value)
         {
              var location = new Location();
              location.Id = inner["Id"]; 
              //etc..
         }      
    }
}

though, it would be much better to create a class structure that you can deserialize your json to, that accurately represents your data, instead of casting Dictionaries.

但是,创建一个可以反序列化json的类结构会更好,它可以准确地表示您的数据,而不是转换Dictionaries。