The following is a example of a JSON array:
{"names":["mrx-prj","mry-prj"],"id":["4-prj","3-prj"]}
How can I read the values in names and id seperately?
It is not a two dimensional array, it is an json object with two values obj.names and obj.id each of which are an array value.
The names array can be accessed using obj.names where names is the key of the array in the json object, same way id array can be accessed via obj.id.
var obj = {"names":["mrx-prj","mry-prj"],"id":["4-prj","3-prj"]}
var names = obj.names;
var ids = obj.id
for(var i = 0; i< names.length; i++){
console.log(names[i]);
//do what ever you want to do with names[i];
}
for(var i = 0; i< ids .length; i++){
console.log(ids [i]);
//do what ever you want to do with ids [i];
}
if your jason is returning from a PHP file the you can try this:
$.getJSON('your-json-string-file.php', function (data) {
$.each(data, function(key, val) {
alert(key +'=>'+ val)
});
});
or you can try this:
$.getJSON('your-json-string-file.php', function (data) {
$.each(data, function(key1, val1) {
$.each(val1, function(key2, val2) {
alert(key2 +'=>'+ val2)
});
});
});
Hope this will help you