8

How do I parse this json with jQuery?

DayEvents: [{
      "0": "886",
      "event_id": "886",
      "1": "5029",
      "user_id": "5029",
      "2": "Professional",
      "user_type": "Professional",
      ...
1
  • Why did you tag this with php ? Commented Jun 9, 2010 at 12:22

2 Answers 2

29

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I wonder why this was down-voted. Seems to be more what the OP was looking for.
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.
+1 simply cause this is a great answer for a beginner! well explaned!
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 :(
7

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.

8 Comments

Note that the OP will need to add braces to make the JSON well-formed.
@jAndy - Please explain. parseJSON() takes a string, right? So how does this answer apply to the question?
What does OP refer to, you and Patrick both used the term, is it an acronym for something?
@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.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.