如何使用$ .each从json_encode获取数据?

时间:2014-02-26 22:19:35

标签: jquery json

如何从这样的数据中获取数据? http://pastebin.com/FGKgbGa7

我的代码:

function getData(){
    $.ajax({ 
        type: 'GET', 
        url: 'process.php', 
        cache: false,
        success : function(result) {
            $.each(result, function(id, item){

            })
        },
        error: function(jqXHR, exception){
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500]');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.')
            } else if (exception === 'timeout') {
                alert('Time out error.')
            } else if (exception === 'abort') {
                alert('Ajax request aborted.')
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
    });
}

如何获取idsnippetupdated_time等...

由于

2 个答案:

答案 0 :(得分:1)

您无需使用.eachfor loop即可。

for (var item in result.data) {
    // do stuff with item
    console.log(item.id);
}

如果您因任何原因想使用.each,它应该看起来像这样

$.each(result.data, function(id, item){
    // print properties
    console.log(item.id);
});

因为result.data是你要循环的数组。

答案 1 :(得分:0)

忘记$.each;只是做:

for(var key in result) {

    // whatever code
    // Example: key is id
    // result[key] is the value of id

}
相关问题