2016-11-29 133 views
0

如何从此json对象访问位置地址的值。如何访问InterJson对象对象值

如何从字符串获取值

{ 
    "total": 1494, 
    "businesses": [ 
     { 
      "price": "$$", 
      "phone": "+19055222999", 
      "name": "Earth To Table : Bread Bar", 
      "url": "https://www.yelp.com/biz/earth-to-table-bread-bar-hamilton?adjust_creative=o3c6gGE-jHIf_ycxKdETJA&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=o3c6gGE-jHIf_ycxKdETJA", 
      "location": { 
       "address1": "Mars", 
       "city": "Toronto", 
       "address3": "", 
       "address2": "", 
       "state": "ON", 
       "country": "CA" 
      } 
     } 
    ] 
} 

我已经试过`

str = JSON.parse(string.businesses.location[0]); 

但它返回string.businesses.location不是一个函数

+3

'STR = JSON.parse(字符串).businesses [0] .location;' – Pointy

+0

亚历山大,你看到如何'BUSIN esses'是一个由基于零的索引访问的数组?你可以用一个循环迭代这个数组,例如'for(var i = 0; i <5; i ++){business [i] .location};' – ThisClark

+0

'someArray.slice(0,5)' –

回答

2

你要转换首先将JSON字符串转换为JSON对象,然后尝试访问数据。

var jsonObj = JSON.parse(jsonString); 
var location = jsonObj.businesses[0].location; 
-1

在JSON,该位置没有“[”等是不是列表,但企业有“[”,所以你应该改变:

str = JSON.parse(string.businesses.location[0]); 

//first parse all the json 
var dataJson = JSON.parse(string); 
//second take te value 
var address = dataJson.businesses[0].location.address1 

Check the example here!!!