Jquery getJSON sorts my data by id automatically

徘徊边缘 提交于 2019-11-30 11:32:38

This is an associative array: order does not matter. The following JSON objects are equivalent:

{
    "3" : "Danie Beach",
    "1" : "Miami",
    "2" : "Weston"
}

{
    "1" : "Miami",
    "2" : "Weston",
    "3" : "Danie Beach"
}

If you need an ordering, you should instead embed an array within the JSON:

{
    "beaches" : [
        {"key" : "3", "value" : "Danie Beach"},
        {"key" : "1", "value" : "Miami"},
        {"key" : "2", "value" : "Weston"}
    ]
}

Example Usage:

jQuery.ajax({
    ...
    dataType: 'json',
    success: function(data) {
        jQuery.each(data.beaches, function(i, beach) {
            alert(i+': beach['+beach.key+'] = '+beach.value);
        });
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!