-6

How would one parse this JSON in jQuery:

{"3":[
 {"project_id":27,"name":"Name1"},
 {"project_id":28,"name":"Name2"},
 {"project_id":29,"name":"Name3"},
 {"project_id":32,"name":"Name4"}
]}
webbydevy
  • 1,200
  • 3
  • 15
  • 21
  • 2
    What did you try? What are you having trouble with? – SLaks Mar 15 '13 at 18:41
  • 2
    What do you mean by "parse"? – J0HN Mar 15 '13 at 18:41
  • 2
    The same way you parse any other JSON. – gen_Eric Mar 15 '13 at 18:43
  • 1
    There's nothing to parse...that's the literal notation for an `object` in JavaScript – BLSully Mar 15 '13 at 18:43
  • Please try to read examples for jQuery.parseJSON and here is a link - http://api.jquery.com/jQuery.parseJSON/ – Piyas De Mar 15 '13 at 18:45
  • 2
    possible duplicate of [How to parse JSON in JavaScript](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript) or maybe [I have a nested data structure / JSON, how can I access a specific value?](http://stackoverflow.com/q/11922383/218196). – Felix Kling Mar 15 '13 at 18:49
  • Sorry, new to parsing json. I didn't realize the insults would flow so easily. thanks to those who took the time to answer the question. I saw the "How to parse JSON in JavaScript" but it wasn't quite working so I thought I'd ask. I thought that that was what this site was for. Here is what the internet has become "Let me post insults because in real world I'm an arrogant self-centered idiot." – webbydevy Mar 15 '13 at 18:57
  • 3
    @ansible: You got quite some negative feedback because it was not very clear what your problem is. The more time you invest in your question and explain your problem, the more positive reactions you usually get. For example, if you had explained that you tried the solution of the other question but that it did not work for you because of X and that you want to do Y, I'm sure the questions wouldn't have been received so badly. Stack Overflow gets lots of low-quality questions and we don't have the time to figure out first what the question is actually about. – Felix Kling Mar 15 '13 at 18:59
  • 2
    @webbydevy: No one is insulting you. Your question was down-voted because you didn't explain yourself very well. You need to do more than just "here's an object, how I use this"? You need to show where the object (in this case, *not* actually JSON) is coming from, how you're trying to use it, what you tried, and what errors you got. We are only going to put effort into answers if you put effort into the question. – gen_Eric Mar 15 '13 at 19:13

4 Answers4

0

The same way we parse JSON every day, Pinky:

var parsed = JSON.parse(input);
Adrian
  • 42,911
  • 6
  • 107
  • 99
0

Generally, you parse JSON using JQuery like this:

var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
hop
  • 2,518
  • 11
  • 40
  • 56
0

I'm not sure what exactly you need but you can access the elements within the object by core JavaScript. You dont need jQuery for that. For example:

theObject[3][0].product_id

would return 27

mavili
  • 3,385
  • 4
  • 30
  • 46
0

JSON data (for example, that returned from an AJAX call) is automatically "parsed" into a structure which is connected to a variable. Or, you can take a string and turn it into a structure using what Adrian showed.

Now, there is one thing about your structure that is problematic: "3" is, as far as I know, not an OK name within an associative array. Change that to "list" or something that meets the requirements of keys like so:

var projects = {"list":[
 {"project_id":27,"name":"Name1"},
 {"project_id":28,"name":"Name2"},
 {"project_id":29,"name":"Name3"},
 {"project_id":32,"name":"Name4"}
]};

Then you could access the elements like so.

console.log(projects.list);
console.log(projects.list[0].project_id, projects.list[0].name);
John Munsch
  • 19,530
  • 8
  • 42
  • 72
  • 1
    Why is `"3"` not an OK name? `var a = {"3": "test"};` works 100% fine (NOTE: You can access objects using `[]`). Also, JavaScript doesn't have "associative arrays", it has "objects". – gen_Eric Mar 15 '13 at 18:50
  • Good question. When I tried it in a jsFiddle with his JSON unchanged, I got an error when trying to access it like so: "console.log(projects.3);" But you can access it like so: console.log(projects["3"]); so it's clearly not "wrong", just a little more cumbersome. So I stand corrected on saying that it's, "Not OK." – John Munsch Mar 15 '13 at 18:55