gpt4 book ai didi

javascript - 如何从数组中选择json项

转载 作者:数据小太阳 更新时间:2023-10-29 05:56:06 27 4
gpt4 key购买 nike

从下面的 JSON 中,如何使用 for 循环和 ajax 检索注释和注释中的标题?

{
"infos": {
"info": [
{
"startYear": "1900",
"endYear": "1930",
"timeZoneDesc": "daweerrewereopreproewropewredfkfdufssfsfsfsfrerewrBlahhhhh..",
"timeZoneID": "1",
"note": {
"notes": [
{
"id": "1",
"title": "Mmm"
},
{
"id": "2",
"title": "Wmm"
},
{
"id": "3",
"title": "Smm"
}
]
},
"links": [
{ "id": "1", "title": "Red House", "url": "http://infopedia.nl.sg/articles/SIP_611_2004-12-24.html" },
{ "id": "2", "title": "Joo Chiat", "url": "http://www.the-inncrowd.com/joochiat.htm" },
{ "id": "3", "title": "Bake", "url": "https://thelongnwindingroad.wordpress.com/tag/red-house-bakery" }
]
}

我尝试了下面的代码,但它不起作用 - 它要么说:

is null

not an object

length is null

r not an object

var detail = eval(xmlhttprequest.responseText)
var rss = detail.infos.info
for(var i = 0; i<rss.length; i++)
startyear += rss[i].startyear

最佳答案

使用

for (i = 0; i < 3; i++) {
alert(JSON.infos.info[0].note.notes[i].title);
}

在这里尝试:JSFIDDLE WORKING EXAMPLE

顺便说一句,您的 JSON 无效。使用此 JSON:

var JSON = {
"infos": {
"info": [
{
"startYear": "1900",
"endYear": "1930",
"timeZoneDesc": "daweerrewereopreproewropewredfkfdufssfsfsfsfrerewrBlahhhhh..",
"timeZoneID": "1",
"note": {
"notes": [
{
"id": "1",
"title": "Mmm"
},
{
"id": "2",
"title": "Wmm"
},
{
"id": "3",
"title": "Smm"
}
]
},
"links": [
{
"id": "1",
"title": "Red House",
"url": "http://infopedia.nl.sg/articles/SIP_611_2004-12-24.html"
},
{
"id": "2",
"title": "Joo Chiat",
"url": "http://www.the-inncrowd.com/joochiat.htm"
},
{
"id": "3",
"title": "Bake",
"url": "https://thelongnwindingroad.wordpress.com/tag/red-house-bakery"
}
]
}
]
}
}

编辑:

这是你想要的:

var infoLength= JSON.infos.info.length;

for (infoIndex = 0; infoIndex < infoLength; infoIndex++) {

var notesLength= JSON.infos.info[infoIndex].note.notes.length;

for (noteIndex = 0; noteIndex < notesLength; noteIndex++) {

alert(JSON.infos.info[infoIndex].note.notes[noteIndex].title);

}
}

关于javascript - 如何从数组中选择json项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6107039/

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