문제

[
    {
        "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.

도움이 되었습니까?

해결책

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

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top