Brian
Brian

Reputation: 622

Get data out of json string

I'm a pretty big n00b with javascript and jquery so I'm trying my best here. I have a JSON return from a Stackmob query. I need to know how to properly get the name, distance, lat and lon from this JSON string so I can use them further on in the google api call.

[
    {
        "name": "eBay",
        "lat_long": {
            "lon": -121.927551,
            "lat": 37.29546,
            "distance": 0.004635090828588474
        },
        "locations_id": "a3563711ebb44a088ae5ab2bd7ff2e0e"
    },
    {
        "name": "Facebook",
        "lat_long": {
            "lon": -121.894955,
            "lat": 37.339386,
            "distance": 0.005523591746943001
        },
        "locations_id": "3fbee8e363bf4247b2e8ca0958c38f4e"
    },
    {
        "name": "Yahoo",
        "lat_long": {
            "lon": -122.025159,
            "lat": 37.417237,
            "distance": 0.006310220058572611
        },
        "locations_id": "d0c568a6322f4a3b8d2fe30cba68f8f1"
    },
    {
        "name": "Hewlett Packard",
        "lat_long": {
            "lon": -122.145989,
            "lat": 37.415515,
            "distance": 0.006312489443257717
        },
        "locations_id": "f03676c3469d4b978bfefcb2ab13790d"
    },
    {
        "name": "Google",
        "lat_long": {
            "lon": -122.083979,
            "lat": 37.421931,
            "distance": 0.006351811575525541
        },
        "locations_id": "56325f71322a4c70ab0e3f1b3fb7fd9b"
    }
]

My return object is model and Ive tried referencing by looping model[i].name etc but get empty strings in firebug.

Upvotes: 0

Views: 1938

Answers (1)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

you could do:

for(var i = 0; i < model.length; i++ ) {
    console.log( model[i].name ); //shows eBay, Facebook, etc
    console.log( model[i].lat_long.lat ); //gives value of lat
    console.log( model[i].lat_long.distance ); //gives distance value for each
}

Demo jsFiddle

Upvotes: 8

Related Questions