2009-10-25 171 views
9

如何使用jQuery訪問Object Literal中的項目名稱?jQuery訪問JSON對象

比如我將如何讀 「稱號」, 「鏈接」, 「媒體」,等...在這個

{ 
    "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": "[email protected]", 
    "tags": "cat persian tnpurrs" 
}, 
+5

順便說一句,這不是一個*關聯數組*是*對象文本* http://is.gd/4BdvZ – CMS 2009-10-25 19:16:45

回答

21

您也可以使用$.each功能:

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

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

如果你去爲for...in狀態換貨方式,我會建議你檢查屬性直接駐留在對象上被迭代,因爲你可以有someissues,如果Object.prototype中被擴展:

for(var key in obj) { 
    if (obj.hasOwnProperty(key)){ 
    // value = obj[key]; 
    } 
} 
9
for (var key in json) { 
    // ... 
} 

(這是標準的JavaScript,而不是jQuery的speficic)

+0

這是正確的,但就像在他的回答中提到@CMS,如果總是重點檢查是json的屬性,而不是原型的屬性,否則可能會遇到問題。只需檢查'if(json.hasOwnProperty(key){// do stuff};'。 – baltoro 2016-01-02 02:17:36

2

在你的情況,這不是一個陣列!你想循環對象的屬性。

JavaScript不真正支持聯想一陽指要麼...看到http://www.hunlock.com/blogs/Mastering_Javascript_Arrays

捷思銳的回答是完美的,我想補充

alert(json[key]); 

裏面的,如果你想知道如何得到的值屬性格式

感謝

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": "[email protected]", 
    "tags": "cat persian tnpurrs" 
}; 

如果是單個對象,那麼你可以通過 testObject.tags或testObject.title ......這樣

訪問此,也可以通過迭代

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

你可以,如果你想不用每次循環訪問。

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