0

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?

3 Answers 3

1

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];
}
Sign up to request clarification or add additional context in comments.

Comments

1

say your object is data .. you can do

 for(var i = 0; i< data.names.length; i++){  //loop to get names
   alert(data.names[i]);
 }

for(var i = 0; i< data.id.length; i++){ //loop to get ids
   alert(data.id[i]);
 }

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.