简体   繁体   English

C#.net 4.帮助从字典对象的子组中提取数据

[英]C# .net 4. Help on extracting data from a daughter group in dictionary object

I am sort of new to C# programming and am having trouble with the dictionary using .net 4.0. 我是C#编程的新手,使用.net 4.0字典时遇到麻烦。

I have sent a JSON object through JavaScriptSerializer into a Dictionary<string, object> object which worked great at extracting all the data. 我已通过JavaScriptSerializer将JSON对象发送到Dictionary<string, object>对象,该对象在提取所有数据时效果很好。

JSON chain JSON链

{ 
    "name" : "MrMonkey", 
    "type" : "monkey", 
    "location" : { 
        "id" : "125235",
        "name" : "zoo" 
    }, 
    "owner" : { 
        "id" : "4211", 
        "name" : "Biggles" 
    }
}

In this dictionary object created I have daughter levels that store information I need to extract from the dictionary and store elsewhere. 在创建的这个字典对象中,我有一个子级别,用于存储需要从字典中提取并存储在其他位置的信息。 Say I want is to extract the location name. 说我要提取的是位置名称。 As you can see it also shares a keyname with the parent level and another daughter level. 如您所见,它还与父级和另一个子级共享一个键名。

For the parent level I can extract information as simply as contact.name = dict["name"].ToString(); 对于父级,我可以像contact.name = dict["name"].ToString();一样简单地提取信息contact.name = dict["name"].ToString(); but how would I go about extracting the required information from the daughter levels? 但是我将如何从子级中提取所需的信息?

I was able to create a work around in JSON.net to get this to work with a little bit of fiddling by checking the datatype and then converting it if it fell within a certain type, but this was aggravating and I been told by the boss not to use JSON.net. 我可以在JSON.net中创建解决方案,通过检查数据类型然后在数据属于某种类型时将其转换,从而使它工作起来有些麻烦,但这很麻烦,老板告诉我不要使用JSON.net。

Without having tried, I'd try something like: (dict["location"] as Dictionary<string,object>)["name"] , as I'd presume from the JSON you've provided, that the daughters themselves are again deserialized into Dictionary s. 不用尝试,我可以尝试: (dict["location"] as Dictionary<string,object>)["name"] ,就像我从您提供的JSON假定的那样,女儿本身就是再次反序列化为Dictionary

Anyway, the debugger will help you a lot here. 无论如何,调试器将在这里为您提供很多帮助。 If you set a breakpoint on the line after the call to the deserialization, you can inspect your dictionary (point the mouse to it and wait a second) and have a look at how your structure is now stored in C# objects. 如果在反序列化调用之后的行上设置了一个断点,则可以检查字典(将鼠标指向该字典并等待一秒钟),然后查看结构现在如何存储在C#对象中。

I would try to create a class that would fill it with the json result. 我会尝试创建一个用json结果填充它的类。 Then work normally and if need be, serialized into json again. 然后正常工作,如果需要,可以再次序列化为json。

public class MyObject
{
  public string name { get;set; }
  public string type { get;set; }
  public Location location { get;set; }
  public Owner owner { get;set; }
}

public class Location
{
  public int id { get;set; }
  public string name { get;set; }
}

public class Owner
{
  public int id { get;set; }
  public string name { get;set; }
}

Have you tried using dict["location"]["name"] ? 您是否尝试使用dict["location"]["name"]

I assume that the daughter level is just treated as another dictionary stored in the parent one... 我假设子级只是被视为父级中存储的另一本词典...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM