开发者

How to decode JSON (varying number of elements, varying keys) using Jquery

开发者 https://www.devze.com 2022-12-31 13:00 出处:网络
My JSON looks like this: { \"person1@email.com\":\"Person开发者_JAVA百科1\", \"person65@email.com\":\"Person65\",

My JSON looks like this:

{
"person1@email.com":"Person开发者_JAVA百科1",
"person65@email.com":"Person65",
"person24@email.com":"Person24"
}

It's returned in various number of elements, and various keys. How do I traverse the data if my code is like this:

$.post("includes/ajax.php", {
    group_id : $('#group').val() 
}, function(data) {
    //how do i traverse data here?
}, "json");

Any help will be appreciated :)

Thanks!


jQuery already parses the JSON data to an object so you can traverse it like this for example:

for(var address in data) {
    var name = data[address];

    alert(name + " " + address);
}

Your data structure is a bit sub-optimal though, you should use multidimensional arrays instead of the emails as key, but that's irrelevant.

0

精彩评论

暂无评论...
验证码 换一张
取 消