dcolumbus
dcolumbus

Reputation: 9722

Accessing the data with dot-syntax

The JSON:

{
    "count": 1,
    "rows": [
        {
            "user_id": "7",
            "lat": "48.452583",
            "lng": "-123.545052",
            "distance": "0.4852177729308128"
        }
    ]
}​

This comes into my function(data) and I'm trying to access count and then the user_id ..

downloadUrl(searchUrl, function(data) {
    alert( "Locations Found: " + data[0].count );
});

data.count, data[0].count ... don't work. And I'm drawing a blank as to how to access them.

Upvotes: 0

Views: 113

Answers (3)

The Alpha
The Alpha

Reputation: 146219

If your data is string (not data-type:json) then you need to parse it as follows

var obj=$.parseJSON(data);

Then use

obj.count;
obj.rows[0].user_id;

Upvotes: 3

beeplogic
beeplogic

Reputation: 478

Is data really an Object or just a string. You can debug type with most browser's console by doing console.log(typeof data) or alternatively alert(typeof data).

Upvotes: 0

Shreedhar
Shreedhar

Reputation: 5640

try this

data = {"count":1,"rows":[{"user_id":"7","lat":"48.452583","lng":"-123.545052","distance":"0.4852177729308128"}]}
data.count
data.rows[0].user_id

Upvotes: 0

Related Questions