How do I access these weird JSON items with jQuery? [duplicate]

廉价感情. 提交于 2020-01-15 05:40:11

问题


Possible Duplicate:
Selecting a JSON object with a colon in the key

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

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.

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

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

Obviously something like

items.y:published.day_name

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?)

(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.)

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.

The solution:

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

回答1:


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

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.




回答2:


object["prop"] is equivalent to object.prop, except that the former is not restricted to valid JavaScript identifiers. (All property names in JavaScript are really strings internally. The latter form is for convenience, but as noted, doesn't always work.)

Happy coding.



来源:https://stackoverflow.com/questions/9644605/how-do-i-access-these-weird-json-items-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!