gpt4 book ai didi

javascript - 遍历 jQuery JSON 对象数组

转载 作者:行者123 更新时间:2023-12-02 18:01:35 25 4
gpt4 key购买 nike

我尝试使用 getJson 访问对象数组,我尝试了很多方法,但总是返回“未定义”或 [Object, object]。

$.getJSON( "js/test.json", function( data ) {
var items = new Array();
$.each( data, function( key, val ) {
items.push( "<li id='" + key + "'>" + val.entries.title + "</li>" );
});

$( "<ul/>", {
"class": "my-new-list",
html: items.join( "" )
}).appendTo( "body" );
});

这是 JSON,我正在尝试获取每个“条目”的“标题”。

{
"$xmlns": {
"pl1": "url"
},
"startIndex": 1,
"author": "MJS",
"entries": [
{
"title": "This is target",
"m$ct": [
{
"m$name": "name"
}
],
"m$rt": "pd",
"m$content": [
{
"plfile$width": 640
},
{
"plfile$width": 960
}
],
"plm$c": [],
"link": ""
},
{
"title": "title2",
"m$ct": [
{
"m$name": "name"
}
],
"m$rt": "pd",
"m$content": [
{
"plfile$width": 640
},
{
"plfile$width": 960
}
],
"plm$c": [],
"link": ""
}
]
}

最佳答案

$.each( data, function( key, val ) {
items.push( "<li id='" + key + "'>" + val.entries.title + "</li>" );
});

应该改为:

$.each( data.entries, function( key, entry ) {
items.push( "<li id='" + key + "'>" + entry.title + "</li>" );
});

关于javascript - 遍历 jQuery JSON 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20484014/

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