如何从数组中选择JSON项目 [英] How to select json item from the array

查看:93
本文介绍了如何从数组中选择JSON项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

{
"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:

为空

不是对象

length为空

r不是对象

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工作示例

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

BTW your JSON is not valid. Use this 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"
                    }
                ]
            }
        ]
    }
}

这是您想要的:

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

    }
}

这篇关于如何从数组中选择JSON项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆