Solved

parsing json in jquery

Posted on 2013-11-21
4
231 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
  • 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 250 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 250 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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)

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now