简体   繁体   English

复杂的json树解析

[英]Complex json tree parsing

So I am given a json tree like the one below and I really just need to get the persons first and last names from here but I am having problems parsing the data. 因此,我得到了一个像下面这样的json树,我真的只需要从这里获取人员的名字和姓氏,但是我在解析数据时遇到了问题。

{
     "results":[
      {
       "id":{
        "key":"Phone.81dd6fef-a2e2-4b08-cfe3-bc7128b43786.Durable",
        "url":"https://proapi.whitepages.com/2.1/entity/Phone.81dd6fef-a2e2-4b08-cfe3-bc7128b43786.Durable.json?api_key=",
        "type":"Phone",
        "uuid":"81dd6fef-a2e2-4b08-cfe3-bc7128b43786",
        "durability":"Durable"
       },
       "line_type":"Landline",
       "belongs_to":[
        {
         "id":{
          "key":"Person.1ffee2ef-cc88-4a1e-87b0-05349571b801.Durable",
          "url":"https://proapi.whitepages.com/2.1/entity/Person.1ffee2ef-cc88-4a1e-87b0-05349571b801.Durable.json?api_key=",
          "type":"Person",
          "uuid":"1ffee2ef-cc88-4a1e-87b0-05349571b801",
          "durability":"Durable"
         },
         "type":"Full",
         "names":[
          {
           "salutation":null,
           "first_name":"fred",
           "middle_name":null,
           "last_name":"jones",
           "suffix":null,
           "valid_for":null
          }
         ],
         "age_range":null,
         "gender":null,
         "locations":[
          {
           "id":{
            "key":"Location.bd4721f0-ba97-4ade-aac1-ed1f16be57ed.Durable",
            "url":"https://proapi.whitepages.com/2.1/entity/Location.bd4721f0-ba97-4ade-aac1-ed1f16be57ed.Durable.json?api_key=",
            "type":"Location",
            "uuid":"bd4721f0-ba97-4ade-aac1-ed1f16be57ed",
            "durability":"Durable"
           },
           "type":"Address",
           "valid_for":{
            "start":{
             "year":2011,
             "month":7,
             "day":5
            },
            "stop":null
           },
           "legal_entities_at":null,
           "city":"",
           "postal_code":"",
           "zip4":null,
           "state_code":"",
           "country_code":"",
           "address":"",
           "house":"10",
           "street_name":"",
           "street_type":"Ave",
           "pre_dir":null,
           "post_dir":null,
           "apt_number":null,
           "apt_type":null,
           "box_number":null,
           "is_receiving_mail":false,
           "not_receiving_mail_reason":null,
           "usage":null,
           "delivery_point":null,
           "box_type":null,
           "address_type":null,
           "lat_long":{
            "latitude":,
            "longitude",
            "accuracy":"Street"
           },
           "is_deliverable":true,
           "standard_address_line1":"",
           "standard_address_line2":"",
           "standard_address_location":"",
           "is_historical":false,
           "contact_type":"Home",
           "contact_creation_date":1361177323
          }
         ],

The code I have been using so far is: 到目前为止,我一直在使用的代码是:

    string url = String.Format("https://proapi.whitepages.com/2.1/phone.json?api_key={0}&phone_number={1}", WhitePagesConstants.ApiKey, number);
    using (WebClient webClient = new System.Net.WebClient())
    {
        WebClient n = new WebClient();
        n.Encoding = System.Text.Encoding.UTF8;
        var json = n.DownloadString(url);
        Dictionary<string, Object> formattedjson = JsonConvert.DeserializeObject<Dictionary<string, Object>>(json);

    }

I have been on this for too long and must finish it shortly so I ask please help me traverse this tree. 我已经进行了太久了,必须尽快完成,所以请帮助我遍历这棵树。 I have never worked with json before and am quite lost on how to do this. 我以前从未使用过json,并且对如何执行此操作相当迷失。 The exact info I need is under "belongs_to" -> "names" -> first and last. 我需要的确切信息是在“属于”->“名称”->第一个和最后一个。 I've changed some names to protect the innocent. 我更改了一些名称以保护无辜者。

If all you need is to extract several properties, you can just navigate the path: 如果您只需要提取几个属性,则可以浏览路径:

dynamic o = JsonConvert.DeserializeObject(json);
Console.WriteLine(o.results[0].belongs_to[0].names[0].first_name);
Console.WriteLine(o.results[0].belongs_to[0].names[0].last_name);

Or, if you prefer string dictionaries over dynamic objects: 或者,如果您更喜欢字符串字典而不是动态对象:

JObject j = JsonConvert.DeserializeObject<JObject>(json);
Console.WriteLine(j["results"][0]["belongs_to"][0]["names"][0]["first_name"]);
Console.WriteLine(j["results"][0]["belongs_to"][0]["names"][0]["last_name"]);

PS Your JSON is broken. PS您的JSON已损坏。 It would have been easier for me to test code if you provided a correct example. 如果您提供了正确的示例,对我来说测试代码会更容易。

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

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