Alex Rivera | Logout

jQuery access JSON Object

Asked 2009-10-25T18:44:58.703
9

How do I access the name of an item in an Object Literal using jQuery?

For example how would I read "title", "link", "media", ect... in this

{
    "title": "What we do in our free time...",
    "link": "http://www.flickr.com/photos/tnhimmies/4042938515/",
    "media": {"m":"http://farm3.static.flickr.com/2572/4042938515_3a00561320_m.jpg"},
    "date_taken": "2009-10-24T03:48:10-08:00",
    "description": "<p><a href=\"http://www.flickr.com/people/tnhimmies/\">Darlene, TN Persians (www.tnpurrs.com)<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/tnhimmies/4042938515/\" title=\"What we do in our free time...\"><img src=\"http://farm3.static.flickr.com/2572/4042938515_3a00561320_m.jpg\" width=\"240\" height=\"230\" alt=\"What we do in our free time...\" /><\/a><\/p> <p>Tennessee Persians<br /> <a href=\"http://www.tnpurrs.com\" rel=\"nofollow\">www.tnpurrs.com<\/a><\/p>",
    "published": "2009-10-25T18:28:36Z",
    "author": "nobody@flickr.com (Darlene, TN Persians (www.tnpurrs.com))",
    "author_id": "66405213@N00",
    "tags": "cat persian tnpurrs"
},
Edit
Report

2 Answers

9
for (var key in json) {
  // ...
}

(this is standard javascript, not jQuery-speficic)

answered 2009-10-25T18:47:43.677
2

In your case, this is not an array at all! You want to loop through properties of an object.

JavaScript does not truly support associative arays either... see http://www.hunlock.com/blogs/Mastering_Javascript_Arrays

Zed's answer is perfect, I would add

alert(json[key]);

inside the for, if you wonder how to get the value of the propery

Thanks

answered 2009-10-25T18:52:25.523

Your Answer