JSON - 如何在数组中获取特定值?

时间:2015-10-13 16:30:04

标签: javascript json api

鉴于以下回复,我如何获得标题之类的内容?

jsonFlickrFeed({
    "title": "Thing",
    "link": "http://www.flickr.com/photos/tags/",
    "description": "",
    "items": [
   {
        "title": "Title",
        "link": "http://www.flickr.com/photos/123",
        "media": {"m":"http://farm6.staticflickr.com/123.jpg"},
   },
   {
        "title": "Title2",
        "link": "http://www.flickr.com/photos/1234",
        "media": {"m":"http://farm6.staticflickr.com/1234.jpg"},
   },
})

我想做类似的事情:

for (var i=0; i<xml.responseText.length;i++) {
    var x = (xml.responseText.items.title[i]);
    document.write(x)
}

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

假设xml.responseText不为null并且是您问题中函数的定义结果... 引用responseText.items长度,而不是responseText。然后将索引从标题移动到项目:

for (var i=0; i < xml.responseText.items.length; i++) {
    var x = xml.responseText.items[i].title;
    document.write(x)
}