How do I access these weird JSON items with jQuery? -
possible duplicate:
selecting json object colon in key
i apologize if duplicate question. searched, did!
what i'm trying achieve simple date re-format nicer "friday, march 9, 2012". love use 1 of many convenient jquery plugins parse readily available "pubdate" value more useful. unfortunately there forces preventing me importing other scripts, including jquery ui. page template mandated superiors imports jquery , that's it.
my json data contains 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 on "items" using $.each, how retrieve values stuff in "y:published"?
obviously like
items.y:published.day_name
doesn't work because of colon. alas, not creator of content (it's json feed yahoo pipe, explain "y:"); i'm tasked manipulating it. i've read, "y:blahblah" entries non-standard json (?) , not parsed via .getjson, in case i'm screwed. (sub-question: assessment correct?)
(and cover bases here: changing yahoo pipe output json rss/xml eliminates "y:published" node altogether, that's not option.)
thanks in advance. have no pride; appreciate forehead-slappingly simple solution, long done straight js or jquery.
update: answered in record time! contributed.
the solution:
var nicedate = singleitem['y:published'].day_name + ', ' + singleitem['y:published'].month_name + ' ' + singleitem['y:published'].day + ', ' + singleitem['y:published'].year;
items
array, first item, use items[0]
.
then access properties on item have invalid identifier names, can use bracketed notation, so:
console.log(items[0]["y:published"].hour); // 21
in javascript, can access object properties in 2 ways: dotted notation , literal (e.g., foo.bar
), or bracketed notation , string (foo["bar"]
). 2 interchangeable, string form property name doesn't have conform rules javascript identifier literals.
Comments
Post a Comment