循环遍历json数组对象

时间:2019-06-10 13:32:59

标签: javascript json nested

如何遍历具有以下json结构的对象的json数组:

"ArrayOfAddress": [{
    "address": {
    "country": "US",
    "city":"NewYork"
    },
    "Type": "435473"
},{
    "address":{
        "country":"Germany",
        "city":"abc"
    },
    "Type":"124586"
}
]

并访问国家,城市和类型值

2 个答案:

答案 0 :(得分:0)

我希望您在将json存储到对象中后尝试访问这些属性。

a = {"ArrayOfAddress": [{
                "address": {
                    "country": "US",
                    "city":"NewYork"
                },
                "Type": "435473"
            },{
                                   "address":{
                                            "country":"Germany",
                                             "city":"abc"
                                         },
                                    "Type":"124586"

                              }

                 ]}
a.ArrayOfAddress.forEach((val) => {document.getElementById("content").innerHTML += (val.address.country + " " + val.address.city + " "+ val.Type) + "<br/>"})
<span id="content"></span>

答案 1 :(得分:0)

什么意思? 您是否希望循环遍历每个叶子值? (国家/地区:美国,城市:NY,类型:435473,国家/地区:德国,城市:abc,类型:123586)?