Solved

Accessing nested JSON data

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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)

746 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

10 Experts available now in Live!

Get 1:1 Help Now