0

This is my JSON output from server side:

[{"id":1,"name":"Information Technology"},
 {"id":2,"name":"mechanical engineering"},
 {"id":3,"name":"computer science"},
 {"id":4,"name":"electronics and communication"}] 

How can I parse this in my client side using jQuery?

1
  • Why do you need to use jQuery? Does the normal Javascript JSON library not work for you? Commented Jan 20, 2012 at 9:01

4 Answers 4

2

IF you're getting it through jquery's getJsonmethod, your object will be automatically be parsed.
A second option will be to parse it yourself through the parseJSON jquery method (http://api.jquery.com/jQuery.parseJSON/) or through the browser's native implementation of JSON parsing : JSON.parse.
The last alternative would be to evaluate the object using eval, but that isn't recommended.

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

3 Comments

It is a way of evaluating things. It's not wrong if you do not have a JSON implementation or if you do not rely on jquery. I know the downsides of using it, I don't recommend it, but it is a possibility. Just because it can present a security risk and it's quite inefficient it doesn't mean that it shouldn't be presented as a valid solution.
Fair enough, but it's probably worth explaining why it should be avoided.
It's not really that relevant for parsing a json with a known structure from a trusted source
1

as you asked specifically for a solution using JQuery you should have a look at the JQuery documentation, here is thier JSON parsing docs Jquery.parseJSON

3 Comments

Depending on the function being used to get the JSON in the first place you might be able to tell it to expect JSON... and therefore not need to use parseJSON.
true, but as we wern't given any information as to how the JSON was being fetched though this was an adequate. :-)
hence why I upvoted it... the additional comment was purely for the user... not as an argument against your response. ;)
1

using jquery it's

var theObject = jQuery.parseJSON(jsonString);

Comments

0

try this

for (var idx in json_object){
 console.log(json_object[idx].id, json_object[idx].name);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.