Solved

parsing json in jquery

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
This article discusses how to implement server side field validation and display customized error messages to the client.
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)

728 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