| Subcribe via RSS


simple example of using json

December 13th, 2007 | No Comments | Posted in ajax, javascript by dreamluverz


Assuming that this array is the return value of ajax and already been evaluated to json.
This is the link on how to do it http://dreamluverz.com/2007/12/12/ajax-and-json/

The {} and [] symbolizes for array

“key” : “value” => ‘:’ colon separates the key and value

var a = {"ids" :[
{"id" : "1", "name" :
["a", "b"]
},
{”id” : “2″, “name” :
[ "ab"]
}
]}

for(var d=0; d < a.ids.length; d++){
alert(a.ids[d].id);
for(var p=0; p < a.ids[d].name.length; p++){
alert(a.ids[d].name[p]);
}
}

Imagine that array in this way using php:

$ids[0]['id'] = 1;
$ids[0]['name'] = array(”a”, “b”);

$ids[1]['id'] = 2;
$ids[1]['name'] = array(”a”);

Tags: