问题
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