如何解析JSON子项

时间:2013-07-29 10:04:48

标签: javascript jquery json codeigniter twitter-bootstrap

这是我得到的回复数据。 有人知道如何解析这些数据吗?

"message":{
       "amount":{"notFloat":"'asd' does not appear to be a float"},
       "lastname":{"notEmpty"}:"lastname must be required"},
       etc....
 }

我想要的是以组织格式显示所有检测到的错误。

options.save(d, {
                success: function (model, response) {
                    if(response['status'] == 'success'){
                        alert = new AlertView({type: 'success', message: 'New record successfully added.'}); 
                    } else {
                         alert = new AlertView({type: 'error', message: response['message']});
                    }
                    alert.render();
                },
                error: function (model, response) {
                    alert = new AlertView({type: 'error', message: response});
                    alert.render();
                }
            });

1 个答案:

答案 0 :(得分:0)

data = $.parseJSON(jsonAsTextFromServer);
$.each( data.message, function( key, value ) {
    $.each( value, function( err_key, err_value ) {
        alert( key + "::" + err_key + ": " + err_value );
    });

});

BTW这种反应是错误的。它应该被{}

包裹

DEMO (Updated)