Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing JSON data

Tags:

json

jquery

If I am given the following data by a web-service:

{
    "d": [
        {
            "col1": "col 1 data 1",
            "col2": "col 2 data 1"
        },
        {
            "col1": "col 1 data 2",
            "col2": "col 1 data 2"
        }
    ]
}

how do I access the second col1?

As the following:

success: function( data ) {
         alert( data.d ) ;
},

gives me:

[object Object],[object Object]
like image 503
oshirowanen Avatar asked Jul 19 '26 09:07

oshirowanen


2 Answers

Its an array with 2 elements containing col1 and col2, so something like:

alert(data.d[1].col1);

(0 is the first element, and then you choose "col1")

like image 170
Nanne Avatar answered Jul 20 '26 23:07

Nanne


success:function(data){
data = JSON.parse(data); // you will have to parse the data first
alert(data.d[0].col1);
like image 43
Rafay Avatar answered Jul 20 '26 22:07

Rafay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!