gpt4 book ai didi

javascript - 如何通过 $.getJSON 访问二级 JSON 对象

转载 作者:行者123 更新时间:2023-11-28 20:57:28 30 4
gpt4 key购买 nike

大家好,请看一下在如下所示的对象内无法达到第二层的问题:

{
"results": [{
"title": "TEAM",
"subtitle": "athletes",
"offset": 0,
"icon": "info",
"relevance": 1,
"link": {
"title": "HOME",
"url": "http://www.test.com",
"id": "23458"
}]
}

这是我的代码:

var theJson = {
init: function() {
this.url = 'http://test.com/js?jsonfeed=123123';
this.fetch();
},

fetch: function() {
$.getJSON(this.url, function( data ){
var obj = $.map(data.results, function( newObj ){
return {
title: newObj .title,
icon: newObj.icon,
topic: newObj.link.id, //??
topic: newObj["link"].id //??
topic: newObj[1].id //??
};
});
});

}
};

theJson.init();

我的问题是如何到达结果对象内链接数组中的 id var?谢谢大家,你们都很摇滚!

最佳答案

您第一次和第二次都做得正确:

newObj.link.id
newObj["link"].id

获取链接下的 id 有两种正确的方法。

关于javascript - 如何通过 $.getJSON 访问二级 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11797390/

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