Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Accessing nested JSON data

Posted on 2014-01-09
4
Medium Priority
?
545 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
  • 2
  • 2
4 Comments
 
LVL 83

Assisted Solution

by:leakim971
leakim971 earned 2000 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 83

Accepted Solution

by:
leakim971 earned 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

885 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