2017-10-08 53 views
0

选择特定的项目我有一个JSON对象,当我这样做:在JSON阵列

console.log(response.json); 

我得到这个

{ results: 
    [ { address_components: [Object], 
     formatted_address: 'Google Bldg 42, 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA', 
     geometry: [Object], 
     place_id: 'ChIJPzxqWQK6j4AR3OFRJ6LMaKo', 
     types: [Object] } ], 
    status: 'OK' } 

我希望能够选择formated_address作为一个例子。我试过console.log(response.json.formatted_address);的变体,但我无法弄清楚。

回答

0

你有一个对象数组里面,所以你需要指定第一项目在数组中。

response.json.results[0].formatted_address 

应该工作。

0

值是不能直接访问,所以你需要做的是这样this.Your的formatted_address位于一个阵列中,存在于结果key.So让你的结果做这样的事情

console.log(response.json.result[0].formatted_address); 
0

尝试这

var response = { results: 
 
    [ { address_components: [Object], 
 
     formatted_address: 'Google Bldg 42, 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA', 
 
     geometry: [Object], 
 
     place_id: 'ChIJPzxqWQK6j4AR3OFRJ6LMaKo', 
 
     types: [Object] } ], 
 
    status: 'OK' } 
 
console.log(response.results[0].formatted_address);

0

刚刚接触的第一个元素,即指数的array然后formatted_address

console.log(response.json.result[0].formatted_address); 
0

您可以使用此代码

console.log(response.json.results[0].formatted_address); 
0