jQuery 访问 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1621685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery access JSON Object
提问by Ben Shelock
How do I access the name of an item in an Object Literal using jQuery?
如何使用 jQuery 访问 Object Literal 中项目的名称?
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"
},
回答by CMS
You can also use the $.eachfunction:
您还可以使用$.each函数:
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 someissues, if the Object.prototype is extended:
如果您采用for...in
语句方式,我建议您检查属性是否直接驻留在被迭代的对象上,因为如果扩展 Object.prototype ,您可能会遇到一些问题:
for(var key in obj) {
if (obj.hasOwnProperty(key)){
// value = obj[key];
}
}
回答by Zed
for (var key in json) {
// ...
}
(this is standard javascript, not jQuery-speficic)
(这是标准的 javascript,不是 jQuery 特定的)
回答by Mike Gleason jr Couturier
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
JavaScript 也不真正支持关联数组……请参阅http://www.hunlock.com/blogs/Mastering_Javascript_Arrays
Zed's answer is perfect, I would add
Zed的回答是完美的,我会补充
alert(json[key]);
inside the for, if you wonder how to get the value of the propery
在 for 里面,如果你想知道如何获得属性的值
Thanks
谢谢
回答by Pyare
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
如果它是单个对象,那么您可以通过 testObject.tags 或 testObject.title 访问它......像这样
or you can iterate by
或者你可以迭代
$.each(obj,function(key,value){
///
});
回答by amilaishere
You can access without each loop if you wanted.
如果需要,您可以在没有每个循环的情况下访问。
var obj = JSON.parse(data);
var title = obj[0].title;