在收到API请求后,JSON中的字典超过了1000册。如何创建遍历所有字典并存储键值对中的 one 值的脚本?
示例
},"testData"
{
"testJSON": "test",
"phone": null,
"address: "122 main st"
}, "testData1"
{
"testJSON": "test1",
"phone": null,
"address: "123 main st"
},
例如,如何获取每个JSON字典的“地址”字段?
答案 0 :(得分:0)
使用forEach循环迭代对象的键
var a={"testData":
{
"testJSON": "test",
"phone": null,
"address": "122 main st"
}, "testData1":
{
"testJSON": "test1",
"phone": null,
"address": "123 main st"
}};
Object.keys(a).forEach(e=>console.log(a[e].address))
答案 1 :(得分:0)
您需要先解析您的json,如下所示:
var json = JSON.parse(jsonString);
然后,您可以迭代json键并使用根值进行操作,例如:
for(var el in json)
{
console.log(json[el]) // will log every root element
console.log(json[el].phone) // will log phone of each element
}
答案 2 :(得分:0)
在python中,可以这样操作:
data = [{
"testJSON": "test",
"phone": null,
"address: "122 main st"
}, "testData1"
{
"testJSON": "test1",
"phone": null,
"address: "123 main st"
}]
my_array = []
for obj in data:
my_array.append(obj['address'])
print(my_array)
希望这就是你所追求的:)