Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a (nested) data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values (or keys) like 1, 105 ,1055?

For example:

[{"1":{"url":"http:\/\/web.com\/","catname":"HOBBIES"}},
{"105":{"parent":"1","url":"http:\/\/web.com\/","catname": "TRUCKS"}},
{"1055":{"parent":"105","url":"http:\/\/web.com\/","catname":"TIRES"}} ]

Code is :

$( document ).ready(function() {
var formURL = 'http://web.com/ajax.php?store=photo&action=jsoncategories';
        $.getJSON( formURL, function(json) {
        $.each(json[0], function(i, object) {
              $.each(object, function(property, value) {
              console.log(property + "=" + value);
              });
           });
        });
});

json[0] is traversing data for key 1. What should replace json[0] to extract all data keys of array

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Here is traversing using JavaScript over your elements. I assume that data was loaded and and traversing starts in your code as follows:

var json = [{"1":{"url":"http:\/\/web.com\/","catname":"HOBBIES"}},
{"105":{"parent":"1","url":"http:\/\/web.com\/","catname": "TRUCKS"}},
{"1055":{"parent":"105","url":"http:\/\/web.com\/","catname":"TIRES"}} ];



for(var i=0, json_len=json.length; i< json_len; i+=1)
{
    var j = json[i]; // Here you are accessing to the item of Array using index of item.
    for(var k in j)
    {
       var d=j[k]; //Here you are accessing to the object using key pair.
       for(var l in d)
           console.log( k, ':', l, ':', d[l]);
    }
}

You can see sample code execution in JSFIDDLY.

If you have long json array this approach better. Because your json variable is Array and traversing among elements of array should be done using for loop. Your items of array is objects so they should be traversed using for ... in. This is best approach to keep in mind. Because for loop is faster than each. Here is comparison in jsperf. If you have question feel free to leave comment here.

share|improve this answer
    
thanx mate for the answer.. but it does not return value(see jsfiddle as well) for k and l while i am replacing console.log to alert( k, ':', l, ':', d[l]); Anything i am missing..pleas let me know –  CodePlayer Jan 6 at 5:18
    
Did you using Chrome? If so, did you open console window? In case if you cannot use console look here. –  Phoenix Jan 6 at 5:22
    
hey thanx..got it.. –  CodePlayer Jan 6 at 5:26
    
Your are Welcome. –  Phoenix Jan 6 at 5:28
add comment

you are missing a " before TRUCK. Also, try using console.log(property + "=" + value); instead of alert().

$.each(json, function(key, val) {
    $.each(val, function(index, value){
         console.log(value);
    });
});

or maybe like so:

$.each(json, function(key, val) {
    $.each(val, function(key, val) {
        console.log(val.url);
    });
});
share|improve this answer
    
thanx for the changes.. this works fine upto traversing. My actual question is: What should replace json[0] to extract all data keys of array –  CodePlayer Jan 6 at 4:32
    
json[0] will only return the 1st value in the array, in this case it is an object. –  manta Jan 6 at 4:45
    
Your last part of suggestion code will not work at all. –  Phoenix Jan 6 at 5:10
    
fixed and answered the question. –  manta Jan 6 at 5:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.