如何访问Inter Json Object Object值

时间:2016-11-29 23:08:51

标签: javascript json

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

如何从字符串

获取值
{
    "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不是函数

2 个答案:

答案 0 :(得分:2)

您必须先将JSON字符串转换为JSON对象,然后尝试访问数据。

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

答案 1 :(得分:-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!!!