0

i would like to how can we parse a json like this using javacsript till the last element using for loop i have completed upto this but i am getting the result as [object,object],[object,object] in the first lart and 2 in second and [object,object] in the third how can alert each and every value in the json array

[
    {
        "location": [
            {
                "building": [
                    "Default Building"
                ],
                "name": "Default Location"
            }
        ],
        "name": "Default Organization"
    },
    {
        "location": [
            {
                "building": [
                    "test_loc1_building1",
                    "test_loc1_building2"
                ],
                "name": "test location1"
            },
            {
                "building": [
                    "test_loc2_building2"
                ],
                "name": "test location2"
            }
        ],
        "name": "test Organization"
    }
]

the code i have been working is

function orgname()
{
    var json = <?php echo $response ?>;
    alert(json);
    alert(json.length);
    for(var i=0; i<json.length; i++)
    {
        var item = json[i];
        alert(item);   
    }
}
Jesse
  • 8,605
  • 7
  • 47
  • 57
dscxz
  • 93
  • 1
  • 1
  • 6
  • 1
    Are you sure it's JSON (i.e. not parsed) ? It looks like plain JS object for the script. – Denys Séguret Apr 11 '13 at 12:16
  • You seem to alert the values right. If you want to see them better, use `console.log` instead of `alert`. – Denys Séguret Apr 11 '13 at 12:18
  • i have used console and founfd that i am passing the json itself form chrome browser – dscxz Apr 11 '13 at 12:19
  • 1
    That's because each top level item is an object, you may want to use console.log(item) instead. – Ja͢ck Apr 11 '13 at 12:20
  • I think this question has been already asked http://stackoverflow.com/questions/8951810/how-to-parse-json-data-with-jquery-javascript – roshan lal Apr 11 '13 at 12:22
  • 2
    possible duplicate of [I have a nested data structure / JSON, how can I access a specific value?](http://stackoverflow.com/questions/11922383/i-have-a-nested-data-structure-json-how-can-i-access-a-specific-value) – Felix Kling Apr 11 '13 at 12:33

2 Answers2

0

Your JSON object is pretty weird. After a bit of reformatting, your JSON looks like:

[
    {
        "location" :
            [
                {
                    "building" : [ "Default Building" ],
                    "name" : "Default Location"
                }
            ],
        "name" : "Default Organization"
    },
    {
        "location" :
            [
                {
                    "building" : [ "test_loc1_building1",  "test_loc1_building2"  ],
                    "name" : "test location1"
                },
                {
                    "building" : [ "test_loc2_building2" ],
                    "name" : "test location2"
                }
            ],
        "name" : "test Organization"
    }
];

There are only two objects (locations?) in the outer array. Of which, the second object contains two buildings. You will either need a double nested loop or recursion to traverse all your buildings.

for (var i=0; i<json.length; i++)
{
    var item = json[i];
    for (var j = 0; j < item.location.length; j++)
    {
        var loc = item.location[j];
        // do stuff here with item and/or loc.
    }
}
Stephen Quan
  • 21,481
  • 4
  • 88
  • 75
0

From your code I judge, that your inserting this directly as a JavaScript object. I assume, you already used json_encode() in the generation of $response.

To actually traverse the whole object then, I'd suggest a recursive approach like this:

var json = <?php echo $response; ?>;

function traverse( obj, cb ) {
  if( Array.isArray( obj ) ) {
    // array in here
    for( var i=0; i<obj.length; i++ ) {
       traverse( obj[i], cb );
    }
  } else if ( typeof obj == 'Object' {
    // object in here
    for( var i in obj ) {
      if( obj.hasOwnProperty( i ) ) {
        traverse( obj[i], cb );
      }
    }
  } else {
    // literal value in here
    cb( obj );
  }

}

traverse( json, alert );

Depending on you actual need you might want to preserve keys or use the callback for at some other point. But the general approach should look similar.

Sirko
  • 72,589
  • 19
  • 149
  • 183