jQuery访问JSON对象 [英] jQuery access JSON Object

查看:148
本文介绍了jQuery访问JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jQuery访问对象文字中的项目名称?

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

推荐答案

您还可以使用 $ .each 函数:

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

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

如果您在 noreferrer> for...in 中查找问题(如果是Object).原型已扩展:

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];
  }
}

这篇关于jQuery访问JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆