Solved

Trying to parse the json data getting both key name and value

Posted on 2013-12-22
1
211 Views
Last Modified: 2013-12-27
I have the following JSON data.  Using JavaScript how do i parse the following JSON data getting the key name and value.   Now the area i am focusing is the DATA node in the JSON

{"total":154,"per_page":20,"current_page":1,"last_page":8,"from":1,"to":20,
"data":[{"id":1,"name":"the name","contact":"Rob"},{"id":2,"name":"the name","contact":"Joe"}]}

Open in new window

0
Comment
Question by:stargateatlantis
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
Test page : http://jsfiddle.net/WVauu/

var json = {"total":154,"per_page":20,"current_page":1,"last_page":8,"from":1,"to":20,
"data":[{"id":1,"name":"the name","contact":"Rob"},{"id":2,"name":"the name","contact":"Joe"}]}

alert("total: " + json.total);

for(var i=0;i<json.data.length;i++) {
    var id = json.data[i]["id"];
    var name = json.data[i].name;
    var contact = json.data[i]["contact"];
    alert("id: " + id + ", name! " + name + ", contact: " + contact);
}

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

762 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

12 Experts available now in Live!

Get 1:1 Help Now