?
Solved

parsing json in jquery

Posted on 2013-11-21
4
Medium Priority
?
240 Views
Last Modified: 2013-11-23
Lets say you back-end returns the following code

[{"key1":1,"key2":2"}]

How would you parse the JSON notice the square brackets
0
Comment
Question by:stargateatlantis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39667670
Please note you've an extra double quote at the end
var a = '[{"key1":1,"key2":2}]';
a = $.parseJSON(a);
var b = $.isArray([{"key1":1,"key2":2}]); // return true if it's an array -> brackets

Open in new window

0
 
LVL 12

Assisted Solution

by:zappafan2k2
zappafan2k2 earned 1000 total points
ID: 39667673
Square brackets indicate an array, curly brackets indicate an object.  This particular array only has one element in it.  So if you have
var data = [{"key1":1,"key2":2}];

Open in new window

you would access the object via data[0].  Then the object's elements can either be accessed like
alert( data[0].key1 );
// or 
alert ( data[0]['key1'] );

Open in new window

Assuming that this is coming from an AJAX call, you might have something like this:
$.get('/my-ajax-script', {term: 'value'}, function(data) {
    alert(data[0].key1);
    alert(data[0].key2);
}, 'json');  // <-- this is what tells jQuery to evaluate the response as a JSON

Open in new window

If you need more help, you should show us the code you have so far, and tell us what you're trying to do with it.
0
 

Author Comment

by:stargateatlantis
ID: 39667693
How would you loop thru the key values
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 1000 total points
ID: 39667709
Test page : http://jsfiddle.net/Q22r5/1/
var a = '[{"key1":1,"key2":2}]';
a = $.parseJSON(a);
var b = $.isArray([{"key1":1,"key2":2}]); // return true if it's an array -> brackets

for(var j in a[0]) {
   var key = j;
   var value = a[0][j]; // or a[0][key]
   alert("key: "+key+"\nvalue: "+value); 
}

Open in new window

0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question