首页 > 解决方案 > 访问某个 json 对象

问题描述

所以我一直在试图通过 JS 中的 for 循环访问某个元素,我正在画一个空白。我有带有 15 个元素的 JSON 对象(下面的一个示例。我正在尝试访问“phones”.phone_number 元素但未定义。下面也是我的 for 循环。

现在我只是控制台记录所有元素。前两个,.name 和 address 回来查找,但它的手机没有返回结果。

for(var i=0; i < json.business.length; i++ ){

  console.log(json.business[i].name);
  console.log(json.business[i].found_at_address.street_line_1);
  console.log(json.business[i].phones[i].phone_number);

}


{
    "count_business": 15,
    "business": [
        {
            "id": "Business.7146cd10-735f-4710-b145-93b97fe45e07",
            "name": "Grace Christian Fellowship",
            "industry": [
                "Religious, Grantmaking, Civic, Professional, and Similar Organizations"
            ],
            "found_at_address": {
                "id": "Location.5c80fbd1-5e5a-4356-9725-5492e3942091",
                "location_type": "Address",
                "street_line_1": "210 2nd St",
                "street_line_2": null,
                "city": "Mounds",
                "postal_code": "62964",
                "zip4": "1144",
                "state_code": "IL",
                "country_code": "US",
                "lat_long": {
                    "latitude": 37.113098,
                    "longitude": -89.200842,
                    "accuracy": "RoofTop"
                },
                "is_active": true,
                "delivery_point": "POBoxThrowback",
                "link_to_business_start_date": "2016-11-19",
                "link_to_business_end_date": null
            },
            "current_addresses": [
                {
                    "id": "Location.5c80fbd1-5e5a-4356-9725-5492e3942091",
                    "location_type": "Address",
                    "street_line_1": "210 2nd St",
                    "street_line_2": null,
                    "city": "Mounds",
                    "postal_code": "62964",
                    "zip4": "1144",
                    "state_code": "IL",
                    "country_code": "US",
                    "lat_long": {
                        "latitude": 37.113098,
                        "longitude": -89.200842,
                        "accuracy": "RoofTop"
                    },
                    "is_active": true,
                    "delivery_point": "POBoxThrowback",
                    "link_to_business_start_date": "2016-11-19"
                }
            ],
            "historical_addresses": [],
            "phones": [
                {
                    "id": "Phone.46c16fef-a2e1-4b08-cfe3-bc7128b6e19a",
                    "phone_number": "+16187459424",
                    "line_type": "Landline"
                }
            ],
            "associated_people": [],
            "associated_businesses": [
                {
                    "id": "Business.e43f1c0d-ecec-42da-ad1c-76badfdf2dcf",
                    "name": "Usda Rural Development",
                    "industry": [
                        "Administration of Housing Programs, Urban Planning, and Community Development"
                    ],
                    "relation": "Household"
                }
            ]
        },

标签: javascriptjsonfor-loop

解决方案


遍历phones数组时需要创建一个单独的循环。由于json.business.length是 15,i将一直到 14。但是,phones.length只有 1,所以phones[14]会抛出错误。

使用下面的代码:

for(var i=0; i < json.business.length; i++ ){
    console.log(json.business[i].name);
    console.log(json.business[i].found_at_address.street_line_1);

    for (var x = 0; x < json.business[i].phones.length; x++) {
        console.log(json.business[i].phones[x].phone_number);
    }
}

注意:更好的方法是使用.forEach()循环。这基本上循环整个数组,比for循环容易得多。

json.business.forEach(business => {
    console.log(business.name);
    console.log(business.found_at_address.street_line_1);

    business.forEach(phones => {
        console.log(phones.phone_number);
    }
})

.forEach()循环基本上做了循环的for作用,但在这种情况下,business将是json.business[i]


推荐阅读