Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Accessing nested JSON data

Posted on 2014-01-09
4
Medium Priority
?
525 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 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 82

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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
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)
Suggested Courses

721 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