gpt4 book ai didi

javascript - 如何在嵌套的 JSON 字符串中循环提取 newsid、headline 的值

转载 作者:行者123 更新时间:2023-11-30 19:14:30 25 4
gpt4 key购买 nike

The Nested JSON - image

{"newsalert":[{"newslist":{"1":{"newsid" :"4321","headline" :"Great White Shark Found","newscode" :"GWS","newstime" :"10:04:32"},"2":{"newsid" :"8031","headline" :"Polar Bear Escaped","newscode" :"PBE","newstime" :"09:28:03"}}}]}


var dt = JSON.parse(json_string);
var value = dt.newsalert[0].newslist.headline;
console.log(value); // Undefined result

我很难弄清楚如何提取数组 newsalert 中的新闻列表。需要提取属性newsid、headline、newscode。

谢谢你:)

最佳答案

不知道你到底想达到什么目的,但这是你访问 json 中的属性的方式

var a={
"newsalert": [{
"newslist": {
"1": {
"newsid": "4321",
"headline": "Great White Shark Found",
"newscode": "GWS",
"newstime": "10:04:32"
},
"2": {
"newsid": "8031",
"headline": "Polar Bear Escaped",
"newscode": "PBE",
"newstime": "09:28:03"
}
}
}]
}

var value = a.newsalert[0].newslist[1].headline;
console.log(value);

关于javascript - 如何在嵌套的 JSON 字符串中循环提取 newsid、headline 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58156298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com