想从json数组中获取特定元素,

时间:2015-09-11 13:36:02

标签: javascript arrays json

我有以下JSON数组,想要从中获取特定元素,例如第一个元素" long_name"

这是我的JSON数组:

({results:[{address_components:[{long_name:"Lahore", short_name:"Lahore", types:["locality", "political"]}, {long_name:"Lahore District", short_name:"Lahore District", types:["administrative_area_level_2", "political"]}, {long_name:"Punjab", short_name:"Punjab", types:["administrative_area_level_1", "political"]}, {long_name:"Pakistan", short_name:"PK", types:["country", "political"]}], formatted_address:"Lahore, Pakistan", geometry:{bounds:{northeast:{lat:31.6332872, lng:74.505512}, southwest:{lat:31.3342113, lng:74.1469001}}, location:{lat:31.55460609999999, lng:74.3571581}, location_type:"APPROXIMATE", viewport:{northeast:{lat:31.6332872, lng:74.505512}, southwest:{lat:31.3342113, lng:74.1469001}}}, place_id:"ChIJ2QeB5YMEGTkRYiR-zGy-OsI", types:["locality", "political"]}], status:"OK"})

请帮助我......谢谢1

5 个答案:

答案 0 :(得分:1)

var data = {results:[{address_components:[{long_name:"Lahore", short_name:"Lahore", types:["locality", "political"]}, {long_name:"Lahore District", short_name:"Lahore District", types:["administrative_area_level_2", "political"]}, {long_name:"Punjab", short_name:"Punjab", types:["administrative_area_level_1", "political"]}, {long_name:"Pakistan", short_name:"PK", types:["country", "political"]}], formatted_address:"Lahore, Pakistan", geometry:{bounds:{northeast:{lat:31.6332872, lng:74.505512}, southwest:{lat:31.3342113, lng:74.1469001}}, location:{lat:31.55460609999999, lng:74.3571581}, location_type:"APPROXIMATE", viewport:{northeast:{lat:31.6332872, lng:74.505512}, southwest:{lat:31.3342113, lng:74.1469001}}}, place_id:"ChIJ2QeB5YMEGTkRYiR-zGy-OsI", types:["locality", "political"]}], status:"OK"};

alert(data.results[0].address_components[0].long_name);

答案 1 :(得分:0)

您需要解析JSON结果,如此

var results = '{"results":[{"address_components":[{"long_name":"Lahore", "short_name":"Lahore", "types":["locality", "political"]}, {"long_name":"Lahore District", "short_name":"Lahore District", "types":["administrative_area_level_2", "political"]}, {"long_name":"Punjab", "short_name":"Punjab", "types":["administrative_area_level_1", "political"]}, {"long_name":"Pakistan", "short_name":"PK", "types":["country", "political"]}], "formatted_address":"Lahore, Pakistan", "geometry":{"bounds":{"northeast":{"lat":31.6332872, "lng":74.505512}, "southwest":{"lat":31.3342113, "lng":74.1469001}}, "location":{"lat":31.55460609999999, "lng":74.3571581}, "location_type":"APPROXIMATE", "viewport":{"northeast":{"lat":31.6332872, "lng":74.505512}, "southwest":{"lat":31.3342113, "lng":74.1469001}}}, "place_id":"ChIJ2QeB5YMEGTkRYiR-zGy-OsI", "types":["locality", "political"]}], "status":"OK"}';

var json = JSON.parse(results);
alert(json.results[0].address_components[0].long_name); //will show 'Lahore'

答案 2 :(得分:0)

JSON对象是列表列表。要访问较低层,首先需要解析JSON对象并创建一个数组     var jsonArray = JSON.parse(json_object);
然后,您需要从您需要访问的数组索引创建一个数组     var address_components = jsonArray[0];
然后你需要再次下台     var long_name = address_components[0].long_name;

答案 3 :(得分:0)

很难在一行上看到JSON的确切路径。将其格式化为更容易观看的人物#34;格式呈现如下:

({
  results: [
    {
      address_components: [
        {
          long_name: "Lahore",
          short_name: "Lahore",
          types: [
            "locality",
            "political"
          ]
        },
        {
          long_name: "Lahore District",
          short_name: "Lahore District",
          types: [
            "administrative_area_level_2",
            "political"
          ]
        },
        {
          long_name: "Punjab",
          short_name: "Punjab",
          types: [
            "administrative_area_level_1",
            "political"
          ]
        },
        {
          long_name: "Pakistan",
          short_name: "PK",
          types: [
            "country",
            "political"
          ]
        }
      ],
      formatted_address: "Lahore, Pakistan",
      geometry: {
        bounds: {
          northeast: {
            lat: 31.6332872,
            lng: 74.505512
          },
          southwest: {
            lat: 31.3342113,
            lng: 74.1469001
          }
        },
        location: {
          lat: 31.55460609999999,
          lng: 74.3571581
        },
        location_type: "APPROXIMATE",
        viewport: {
          northeast: {
            lat: 31.6332872,
            lng: 74.505512
          },
          southwest: {
            lat: 31.3342113,
            lng: 74.1469001
          }
        }
      },
      place_id: "ChIJ2QeB5YMEGTkRYiR-zGy-OsI",
      types: [
        "locality",
        "political"
      ]
    }
  ],
  status: "OK"
})

通过这种方式,您可以看到json对象包含结果的属性,该属性是一个数组。结果元素包含address_components属性,该属性也是一个数组。 address_components的元素包含long_name属性。要访问它,您将执行以下操作,假设您有一个存储在jsonString中的JSON字符串:

var jsonObject = JSON.parse(jsonString);
var firstLongNameProperty = jsonObject.results[0].address_components[0].long_name;

答案 4 :(得分:0)

首先你应该构建一个有效的JSON字符串,这样你就需要在你的键周围加上引号......

'{"results": [{"address_components": [{"long_name": "Lahore","short_name": "Lahore","types": ["locality","political"]},{"long_name": "Lahore District","short_name": "Lahore District","types": ["administrative_area_level_2","political"]},{"long_name": "Punjab","short_name": "Punjab","types": ["administrative_area_level_1","political"]},{"long_name": "Pakistan","short_name": "PK","types": ["country","political"]}],"formatted_address": "Lahore, Pakistan","geometry": {"bounds": {"northeast": {"lat": 31.633287,"lng": 74.50551},"southwest": {"lat": 31.334211,"lng": 74.1469}},"location": {"lat": 31.554605,"lng": 74.357155},"location_type": "APPROXIMATE","viewport": {"northeast": {"lat": 31.633287,"lng": 74.50551},"southwest": {"lat": 31.334211,"lng": 74.1469}}},"place_id": "ChIJ2QeB5YMEGTkRYiR-zGy-OsI","types": ["locality","political"]}],"status": "OK"}'

Better View JSON

之后,您只需要解析JSON文本并在需要的地方附加值

HTML

<div id="test"></div>

的JavaScript

var obj = JSON.parse(text);
var text_div = document.getElementById('test');
for (i = 0; i < obj.results[0].address_components.length; i++) {
  text_div.innerHTML = text_div.innerHTML + obj.results[0].address_components[i].long_name + '<br>';
}

JSFIDDLE