Solved

Accessing nested JSON data

Posted on 2014-01-09
4
509 Views
Last Modified: 2014-01-09
Hello,

I have JSON but am unable to get the nested data.  The example of my JSON is below.  It is valid JSON.  I have the javascript/jquery code I am trying to use below to access the data.  I would like to be able to get all the data for id=1.  The console log is this:

0 Object { id="1", abbr="en", data=[2]}
1 Object { id="2", abbr="fr", data=[2]}

How do I access abbr (tmpObj.abbr comes up undefined) and the "textboxID" and "text" for each element in 'data'?

Many thanks in advance!

Cheers,



{
    "rows": [
        {
            "id": "1",
            "abbr": "en",
            "data": [
                {
                    "textboxID": "1",
                    "text": "This is English"
                },
                {
                    "textboxID": "2",
                    "text": "English 2"
                }
            ]
        },
        {
            "id": "2",
            "abbr": "fr",
            "data": [
                {
                    "textboxID": "1",
                    "text": "C'est en francais."
                },
                {
                    "textboxID": "2",
                    "text": "Francais 2"
                }
            ]
        }
    ]
}

Open in new window



$.getJSON( url, function(result) {
alert( "Success" );
var tmpObj = $.grep(result.rows, function(obj){
    return obj.id == '1'
});
$.each(result, function(key, value){
    $.each(value, function(key, value){
        console.log(key, value);
    });
});
});

Open in new window

0
Comment
Question by:1Cougar
[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
  • 2
4 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 500 total points
ID: 39768172
Test page : http://jsfiddle.net/6ZcWW/

$.getJSON( url, function(result) {
alert( "Success" );

var tmpObj = $.grep(result.rows, function(obj){
    return obj.id == '1'
});

tmpObj = tmpObj[0];

var id = tmpObj.id;
var abbr = tmpObj.abbr;
var data = tmpObj.data;
for(var i=0;i<data.length;i++) {
    var textboxID = data[i].textboxID;
    var text = data[i].text;
    console.log(textboxID + " : " + text);
}
});

Open in new window

0
 

Author Comment

by:1Cougar
ID: 39768193
Hello,

Thank you!---how would I go about accessing the "text" only for "textboxID=2"?

Thanks again,
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39768222
http://jsfiddle.net/6ZcWW/1/

alert( "Success" );

var tmpObj = $.grep(result.rows, function(obj){
    return obj.id == '1'
});

tmpObj = tmpObj[0];

var id = tmpObj.id;
var abbr = tmpObj.abbr;
var data = tmpObj.data;

tmpObj = $.grep(data, function(obj){
    return obj.textboxID == '2'
});

data = tmpObj[0];

var textboxID = data.textboxID;
var text = data.text;
console.log(textboxID + " : " + text);

Open in new window

0
 

Author Comment

by:1Cougar
ID: 39768233
Thank you so much!  Just what I needed!

Cheers,
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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…

617 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