tags:

views:

117

answers:

2

How do I parse this json with jQuery?

DayEvents : [{"0":"886","event_id":"886","1":"5029","user_id":"5029","2":"Professional","user_type":"Professional", ...
+3  A: 

Stolen from .parseJSON() doc.

Example:

Parse a JSON string.

var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

Your example code seems to be an object already. You would have the put braces around the whole thing to use and parse it with parseJSON.

jAndy
Note that the OP will need to add braces to make the JSON well-formed.
tvanfosson
@jAndy - Please explain. `parseJSON()` takes a string, right? So how does this answer apply to the question?
patrick dw
What does OP refer to, you and Patrick both used the term, is it an acronym for something?
Simon Lomax
@jAndy: http://api.jquery.com/jQuery.parseJSON/
RobertPitt
@Simon: parsing is transforming one unuseable format into another useable format. If it were a large string (with leading and trailing quote) then you couldn't access it by `jsonObj.someProperty`, so you would like to parse it first. But the code piece the OP posted is already in JSON, he just don't know how to access it.
BalusC
@patrick: I'm assuming that the OP means just like that, parsing a `JSON` string into a `javascript` object. His example code is maybe missleading, but I stick to what he described `"How do I parse this json with jQuery?"`
jAndy
@BalusC - no you misunderstand. I didn't know what the acronum *OP* stood for. I 've just worked out that it must mean Original Poster (i.e. The person who posted the original message). Acronyms are great - when you know what they mean. TTFN :-)
Simon Lomax
@Simon: Oh you went offtopic, nevermind. @jAndy: it's however a pretty common misphrase. Regardless, let's see what the OP says after all.
BalusC
@jAndy - I may be wrong, but I think the opposite is true. I think it is a mis-use of the word `parse`. Because OP already has a valid JSON object, there's no need to parse. I think OP may want to access properties.
patrick dw
+7  A: 

The term "parsing" is a bit misplaced since this is already in JSON format. You don't need to parse it, but just to access it. If it were a large String in JSON format then you indeed need to parse it into an useable JSON object first before accessing.

This JSON contains one property, the DayEvents, which in turn contains an array []. You can access properties using dot . operator. You can get an array item at the given index using [index] where zero 0 denotes the first item.

var json = { DayEvents : [{"0":"886","event_id":"886","1":"5029","user_id":"5029","2":"Professional","user_type":"Professional" }]};
var firstDayEvent = json.DayEvents[0];

The array in turn contains an object {}. Or maybe more than one? You can have more than one items in an array, you should then see [{}, {}, {}, ...] and you could then access each item in an loop like so:

for (var i = 0; i < json.DayEvents.length; i++) {
    var dayEvent = json.DayEvents[i];
    // ...
}

A single day event object has several properties: 0, event_id, 1, user_id, 2, etc. You cannot access properties starting with a number using dot . operator, you would then like to use the brace notation:

var zero = firstDayEvent['0'];
var eventId = firstDayEvent.event_id;
var one = firstDayEvent['1'];
var userId = firstDayEvent.user_id;
var two = firstDayEvent['2'];
// ...

alert(eventId); // 886
alert(two); // Professional

To learn more about JSON, check this tutorial.

BalusC
I wonder why this was down-voted. Seems to be more what the OP was looking for.
patrick dw
@patrick: ach, you have nitpickers.
BalusC
Well, I've got to give you a +1, because the answer that everyone is voting for doesn't make any sense to me. It's just a copy/paste of an example from the docs.
patrick dw
+1 simply cause this is a great answer for a beginner! well explaned!
aSeptik
For some reason i was getting alerts about this it seems it was me that downvoted it, i cant remember doing this :/ i upvoted anyways. sorry :(
RobertPitt