Question

[
    {
        "id":"26",
        "latitude":"10.308749502342007",
        "longitude":"123.88429984649656"
    },
    {
        "id":"28",
        "latitude":"10.313816172726275",
        "longitude":"123.89030799468992"
    }
]

I have this json array from php and I want to alert the id which is 26. How can I do that.I am using $.ajax. Any help would be highly appreciated.

Thanks in advance.

Was it helpful?

Solution

You should use JSON.parse to convert JSON string to javascript Object

var json_string = '[{"id":"26","latitude":"10.308749502342007","longitude":"123.88429984649656"},{"id":"28","latitude":"10.313816172726275","longitude":"123.89030799468992"}]';
var result = JSON.parse(json_string);
alert(result[0].id); //Will alert 26

OTHER TIPS

Maybe you want to access element by id:

function parseData(data) {
    var result = {};
    $(data).each(function(i, el) {
        result[el.id] = el;
    });
    return result;
}

// ...

data  = parseData(response); // response from $.ajax
alert(data['26'].latitude); // access element by id
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top