Solved

parsing json in jquery

Posted on 2013-11-21
4
238 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 Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Javascript/JQuery - HtmlEncode (?) Ajax Result 1 81
PHP and JQuery Syntax question 4 34
Ahax pagination 9 48
GitHub 1 9
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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)

733 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