复杂的json树解析

反抗

因此,我得到了一个像下面这样的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
          }
         ],

到目前为止,我一直在使用的代码是:

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

    }

我在这上面呆了太久了,必须尽快完成,所以请您帮我遍历这棵树。我以前从未使用过json,并且在如何执行此操作上一无所获。我需要的确切信息是在“属于”->“名称”->第一个和最后一个。我更改了一些名称以保护无辜者。

影响

如果您只需要提取几个属性,则可以浏览路径:

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

或者,如果您更喜欢字符串字典而不是动态对象:

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您的JSON已损坏。如果您提供了正确的示例,对我来说测试代码会更容易。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章