Solved

Accessing nested JSON data

Posted on 2014-01-09
4
507 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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…
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