Javascript 如何从数组中选择json项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6107039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:14:48  来源:igfitidea点击:

How to select json item from the array

javascriptajaxjson

提问by CutexBabies

From the below JSON, how can I retrieve title from the note and notes using a for loop and ajax to retrieve?

从下面的 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" }
            ]
        }

I tried out the code below but it doesn't work - it either says:

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

is null

not an object

length is null

r not an object

一片空白

不是对象

长度为空

r 不是对象

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

回答by Kerem Baydo?an

Use

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

TRY IT HERE: JSFIDDLE WORKING EXAMPLE

在这里尝试:JSFIDDLE 工作示例

BTW your JSON is not valid. Use this JSON:

顺便说一句,您的 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"
                    }
                ]
            }
        ]
    }
}

EDIT:

编辑:

Here is what you want:

这是你想要的:

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);

    }
}

回答by Gary Green

Well the "path" to the JSON notesarray-like object is:

那么 JSON 类notes数组对象的“路径”是:

json.infos.info[0].note.notes;

So you could do something like:

所以你可以这样做:

var notes = json.infos.info[0].note.notes;
var titles = [];
for (var i = 0, len = notes.length; i < len; i++)
{
   titles.push(notes[i].title);
}

alert('titles is: ' + titles.join(', '));

Fiddle:http://jsfiddle.net/garreh/uDxqD/

小提琴:http : //jsfiddle.net/garreh/uDxqD/



Are you using jQuery? ;-)

你在使用 jQuery 吗?;-)

// Assuming your using "success" in ajax response
success: function(json)
{
    var titles = $(json.infos.info[0].note.notes).map(function() {
        return this.title;
    }).get();
    alert(titles.join(', '));
}

回答by Thor Jacobsen

Putting your json into an var called obj, use the following:

将您的 json 放入名为 的 var 中obj,使用以下命令:

obj.infos.info[0].note.notes[0].title

http://jsfiddle.net/Znq34/

http://jsfiddle.net/Znq34/

回答by Sushant Prasad

First count the length of notes

先算笔记的长度

var len = jsonobject.infos.info.note.notes.length;

var len = jsonobject.infos.info.note.notes.length;

Then loops through and get

然后循环并得到

var title = jsonobject.infos.info.note.notes[i].title;

var title = jsonobject.infos.info.note.notes[i].title;