j_d
j_d

Reputation: 3082

JSON - How to get specific value inside array?

Given the below response, how would I get things like title?

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"},
   },
})

I want to do something like:

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

What am I doing wrong here?

Upvotes: 2

Views: 93

Answers (1)

hubson bropa
hubson bropa

Reputation: 2770

assuming xml.responseText is not null and is the defined result of the function in your question... reference the responseText.items length, not responseText. Then move your index from title to items:

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

Upvotes: 1

Related Questions