如何从数组中选择json项

| 从下面的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\" }
            ]
        }
我尝试了下面的代码,但是它不起作用-它要么说:   一片空白      不是对象      长度为空      不是对象
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:
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);

    }
}
    
将您的json放入名为
obj
的var中,请使用以下命令:
obj.infos.info[0].note.notes[0].title
http://jsfiddle.net/Znq34/     
JSON
notes
数组对象的\“ path \”是:
json.infos.info[0].note.notes;
因此,您可以执行以下操作:
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(\', \'));
小提琴:http://jsfiddle.net/garreh/uDxqD/ 您正在使用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(\', \'));
}
    
先算一下笔记的长度
var len = jsonobject.infos.info.note.notes.length;
然后循环获取
var title = jsonobject.infos.info.note.notes[i].title;
    

要回复问题请先登录注册