从JSON对象解析键值

时间:2017-05-25 16:08:27

标签: javascript json parsing object

我有一个从geocod.io API调用收到的JSON对象。我需要解析对象并得到键值,特别是键(例如“246212”)和lat和lng

    {  
   "results":{  
      "246212":{  
         "query":"1601 Sandhurst Dr, Waxahachie, Texas",
         "response":{  
            "input":{  
               "address_components":{  
                  "number":"1601",
                  "street":"Sandhurst",
                  "formatted_street":"Sandhurst",
                  "city":"Waxahachie",
                  "state":"TX",
                  "zip":"75165",
                  "country":"US"
               },
               "formatted_address":"1601 Sandhurst, Waxahachie, TX 75165"
            },
            "results":[  
               {  
                  "address_components":{  
                     "number":"1601",
                     "street":"Sandhurst",
                     "formatted_street":"Sandhurst",
                     "city":"Waxahachie",
                     "county":"Ellis County",
                     "state":"TX",
                     "zip":"75165",
                     "country":"US"
                  },
                  "formatted_address":"1601 Sandhurst, Waxahachie, TX 75165",
                  "location":{  
                     "lat":32.42555,
                     "lng":-96.833181
                  },
                  "accuracy":1,
                  "accuracy_type":"range_interpolation",
                  "source":"TIGER\/Line\u00ae dataset from the US Census Bureau"
               }
            ]
         }
      },
      "246994":{  
         "query":"1415 Willow Run Dr, Duncanville, Texas",
         "response":{  
            "input":{  
               "address_components":{  
                  "number":"1415",
                  "street":"Willow Run",
                  "suffix":"Dr",
                  "formatted_street":"Willow Run Dr",
                  "city":"Duncanville",
                  "state":"TX",
                  "zip":"75137",
                  "country":"US"
               },
               "formatted_address":"1415 Willow Run Dr, Duncanville, TX 75137"
            },
            "results":[  
               {  
                  "address_components":{  
                     "street":"Willow Run",
                     "suffix":"Dr",
                     "formatted_street":"Willow Run Dr",
                     "city":"Duncanville",
                     "county":"Dallas County",
                     "state":"TX",
                     "zip":"75137",
                     "country":"US"
                  },
                  "formatted_address":"Willow Run Dr, Duncanville, TX 75137",
                  "location":{  
                     "lat":32.635994,
                     "lng":-96.884205
                  },
                  "accuracy":0.8,
                  "accuracy_type":"street_center",
                  "source":"TIGER\/Line\u00ae dataset from the US Census Bureau"
               }
            ]
         }
      },
      "247037":{  
         "query":"1800 Windmill Hill Ln, Desoto, Texas",
         "response":{  
            "input":{  
               "address_components":{  
                  "number":"1800",
                  "street":"Windmill Hill",
                  "suffix":"Ln",
                  "formatted_street":"Windmill Hill Ln",
                  "city":"Desoto",
                  "state":"TX",
                  "zip":"75115",
                  "country":"US"
               },
               "formatted_address":"1800 Windmill Hill Ln, Desoto, TX 75115"
            },
            "results":[  
               {  
                  "address_components":{  
                     "number":"1800",
                     "street":"Windmill Hill",
                     "suffix":"Ln",
                     "formatted_street":"Windmill Hill Ln",
                     "city":"Desoto",
                     "county":"Dallas County",
                     "state":"TX",
                     "zip":"75115",
                     "country":"US"
                  },
                  "formatted_address":"1800 Windmill Hill Ln, Desoto, TX 75115",
                  "location":{  
                     "lat":32.613862,
                     "lng":-96.907534
                  },
                  "accuracy":1,
                  "accuracy_type":"range_interpolation",
                  "source":"TIGER\/Line\u00ae dataset from the US Census Bureau"
               },
               {  
                  "address_components":{  
                     "number":"1800",
                     "street":"Windmill Hill",
                     "suffix":"Ln",
                     "formatted_street":"Windmill Hill Ln",
                     "city":"Desoto",
                     "county":"Dallas County",
                     "state":"TX",
                     "zip":"75115",
                     "country":"US"
                  },
                  "formatted_address":"1800 Windmill Hill Ln, Desoto, TX 75115",
                  "location":{  
                     "lat":32.614162,
                     "lng":-96.907523
                  },
                  "accuracy":0.8,
                  "accuracy_type":"range_interpolation",
                  "source":"TIGER\/Line\u00ae dataset from the US Census Bureau"
               }
            ]
         }
      }
   }
}

我搜索并尝试了以下代码段

// this converts to object
var obj = JSON.parse(geocodedAddress;

console.log('obj ', obj);

for (var key in obj) {
    let value = obj[key];
    console.log('value ', value);
}

console.log('Object.keys ', Object.keys(obj.results));


// This is not returning anything
var getKeys = function(obj) {
    var keys = [];
    for (var key in obj) {
        keys.push(key);
        console.log('key ', key);
    }
    console.log('keys ', keys);
    return keys;
};

我认为我很接近,你可以从截图中看到。任何帮助将非常感激。 enter image description here

0 个答案:

没有答案