Solved

parsing json in jquery

Posted on 2013-11-21
4
237 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

856 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