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": "[email protected] (Darlene, TN Persians (www.tnpurrs.com))",
    "author_id": "66405213@N00",
    "tags": "cat persian tnpurrs"
},
1
  • 5
    BTW, it isn't an associative array is an Object Literal is.gd/4BdvZ Commented Oct 25, 2009 at 19:16

5 Answers 5

21

You can also use the $.each function:

var obj = { one:1, two:2, three:3, four:4, five:5 };

$.each(obj, function(key, value) {
  //..
});

If you go for the for...in statement way, I would recommend you to check if the property resides directly on the object being iterated, because you could have some issues, if the Object.prototype is extended:

for(var key in obj) {
  if (obj.hasOwnProperty(key)){
    // value = obj[key];
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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

(this is standard javascript, not jQuery-speficic)

1 Comment

This is correct but just like @CMS mentioned in his answer, always check if key is a property of json and not property of prototype, otherwise you can run into issues. Simply check if (json.hasOwnProperty(key) { //do stuff};.
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

Comments

0

You can access without each loop if you wanted.

var obj = JSON.parse(data);
var title  = obj[0].title;

Comments

0
var testObject = {
    "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": "[email protected] (Darlene, TN Persians (www.tnpurrs.com))",
    "author_id": "66405213@N00",
    "tags": "cat persian tnpurrs"
};

if it is single object then you can access this by testObject.tags or testObject.title ... like this

or you can iterate by

$.each(obj,function(key,value){
///
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.