2

I'm trying to read the value of a json nested object.
I have tried in these ways but without success.

data

{ "foo":  1,
  "bar": 10,
  "rows": [ {"id":1,"name":"Luke" },
            {"id":2,"name":"Sky" },
            {"id":3,"name":"Walker"} ]
}

Ajax

$.ajax({
    data: params,
    dataType: 'json',
    success: function(data) {

        console.log(data.rows) // data of rows object - OK !
        console.log(data["rows"].id); // undefined
        console.log(data.rows.id); // undefined
    }
});

How could I do? Thank you

2

2 Answers 2

2

Rows is an array.

Change

console.log(data["rows"].id);

to

console.log(data["rows"][0].id);

You can also iterate on it to get all the values

for (var i = 0; i < data["rows"].length; i++) {
    console.log(data["rows"][i].id); 
}
Sign up to request clarification or add additional context in comments.

Comments

1

Data.rows is array of objects It should be accessed in other way

 $.ajax({
        data: params,
        dataType: 'json',
        success: function(data) {

            console.log(data.rows) // data of rows object - OK !
          $.each( data.rows, function( key, value ) {
      console.log( key + ": " + value );
       });

        }
    });

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.