如何使用jQuery访问这些奇怪的JSON项? [英] How do I access these weird JSON items with jQuery?

查看:127
本文介绍了如何使用jQuery访问这些奇怪的JSON项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

选择一个带有冒号的JSON对象

如果这是一个重复的问题,我道歉。我搜索过,我真的做到了!

I apologize if this is a duplicate question. I searched, I really did!

我想要实现的是一个简单的日期重新格式化为更好的东西,如2012年3月9日星期五。我很乐意使用众多方便的jQuery插件之一来将现成的pubDate值解析成更有用的东西。不幸的是,有些力量阻止我导入任何其他脚本,包括jQuery UI。我的上司强制要求的页面模板导入jQuery,就是这样。

What I'm trying to achieve is a simple date re-format into something nicer like "Friday, March 9, 2012". I would LOVE to use one of the many convenient jQuery plugins to parse the readily available "pubDate" value into something more useful. Unfortunately there are forces preventing me from importing any other scripts, including jQuery UI. The page template mandated by my superiors imports jQuery and that's it.

我的JSON数据包含以下代码段:

My JSON data contains the following snippet:

"items": [
            {
                "title": "blah blah", 
                "link": "http://url.blah.com", 
                "category": "category blah", 
                "pubDate": "Fri, 09 Mar 2012 16:16:05 -0500", 
                "y:published": {
                    "hour": "21", 
                    "timezone": "UTC", 
                    "second": "5", 
                    "month": "3", 
                    "month_name": "March", 
                    "minute": "16", 
                    "utime": "1331327765", 
                    "day": "9", 
                    "day_ordinal_suffix": "th", 
                    "day_of_week": "5", 
                    "day_name": "Friday", 
                    "year": "2012"
                }, 
                "y:id": {
                    "permalink": "true", 
                    "value": null
                }, 
                "y:title": "blah blah", 
                "description": "more blah blah"
            }



<如果我使用$ .each循环items,如何在y:published中检索内容的值?

If I'm looping over "items" using $.each, how do I retrieve the values for stuff in "y:published"?

显然类似于

items.y:published.day_name

$ b由于冒号,
$ b

不起作用。唉,我不是这个内容的创建者(它实际上是来自Yahoo Pipe的JSON提要,可能会解释y:);我只是负责操纵它。根据我的阅读,y:blahblah条目是非标准的JSON(?),可能不是通过.getJSON解析的,在这种情况下我搞砸了。 (子问题:这个评估是否正确?)

doesn't work because of the colon. Alas, I am not the creator of this content (it's actually the JSON feed from a Yahoo Pipe, which would probably explain the "y:"); I'm simply tasked with manipulating it. From what I've read, the "y:blahblah" entries are non-standard JSON (?) and probably not parsed via .getJSON, in which case I'm screwed. (Sub-question: is this assessment correct?)

(这就是我在这里覆盖所有基础:将Yahoo Pipe输出从JSON更改为RSS / XML消除了y:发布节点,所以这不是一个选项。)

(And just so I cover all my bases here: changing the Yahoo Pipe output from JSON to RSS/XML eliminates the "y:published" node altogether, so that's not an option.)

提前致谢。我没有骄傲;我甚至会欣赏最前沿的简单解决方案,只要可以使用直接js或jQuery完成。

Thanks in advance. I have no pride; I would appreciate even the most forehead-slappingly simple solution, as long as it could be done with straight js or jQuery.

更新:以创纪录的时间回答!感谢所有贡献者。

UPDATE: Answered in record time! Thanks to everyone who contributed.

解决方案:

var niceDate =
singleItem['y:published'].day_name + ', ' +
singleItem['y:published'].month_name + ' ' +
singleItem['y:published'].day + ', ' +
singleItem['y:published'].year;


推荐答案

items 是一个数组,所以要获得第一项,你使用 items [0]

items is an array, so to get the first item, you use items[0].

然后要访问该项目上具有无效标识符名称的属性,您可以使用括号表示法,因此:

Then to access the properties on that item that have invalid identifier names, you can use bracketed notation, so:

console.log(items[0]["y:published"].hour); // 21

在JavaScript中,您可以通过两种方式访问​​对象属性:带点分表示法和文字(例如, foo.bar ),或括号括号和字符串( foo [bar] )。这两个是完全可互换的,但是使用字符串形式,属性名称不必符合JavaScript标识符文字的规则。

In JavaScript, you can access object properties in two ways: With dotted notation and a literal (e.g., foo.bar), or with bracketed notation and a string (foo["bar"]). The two are completely interchangeable, but with the string form the property name doesn't have to conform to the rules for JavaScript identifier literals.

这篇关于如何使用jQuery访问这些奇怪的JSON项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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