我希望解析一个json url /文件,只获取json中的顶级节点。
json对象如下所示:
{
"2017080350": {
"home": {
"score": {
"1": null,
"2": null,
"3": null,
"4": null,
"5": null,
"T": null
},
"abbr": "ARI",
"to": null
},
"away": {
"score": {
"1": null,
"2": null,
"3": null,
"4": null,
"5": null,
"T": null
},
"abbr": "DAL",
"to": null
},
"bp": 0,
"down": null,
"togo": null,
"clock": null,
"posteam": null,
"note": null,
"redzone": null,
"stadium": "Tom Benson Hall of Fame Stadium",
"media": {
"radio": {
"home": null,
"away": null
},
"tv": "NBC",
"sat": null,
"sathd": null
},
"yl": null,
"qtr": null
},
"2017080351": {
"home": {
"score": {
"1": null,
"2": null,
"3": null,
"4": null,
"5": null,
"T": null
},
"abbr": "ARI",
"to": null
},
"away": {
"score": {
"1": null,
"2": null,
"3": null,
"4": null,
"5": null,
"T": null
},
"abbr": "DAL",
"to": null
},
"bp": 0,
"down": null,
"togo": null,
"clock": null,
"posteam": null,
"note": null,
"redzone": null,
"stadium": "Tom Benson Hall of Fame Stadium",
"media": {
"radio": {
"home": null,
"away": null
},
"tv": "NBC",
"sat": null,
"sathd": null
},
"yl": null,
"qtr": null
}
}
我想要得到的是两个值2017080350
和2017080351
我可以正常阅读和显示内容,但在尝试获取两个提到的节点时遇到问题?
答案 0 :(得分:1)
如果您的回复尚未解析,请先执行以下操作:
response = JSON.parse(response);
然后你可以使用for...in
循环:
for (let key in response) {
console.log(key);
}
要读取链接到该键的数据,您可以像这样展开:
for (let key in response) {
console.log(key);
let value = response[key];
// and you can go deeper...
for (let deeperKey in value) {
// ...etc.
}
}