【问题标题】:How to parse JSON subitem如何解析 JSON 子项
【发布时间】:2013-07-29 01:45:44
【问题描述】:

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

"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();
                }
            });

【问题讨论】:

  • 这是无效的 Json,请使用 jsonlint.com 进行检查
  • 那不是有效的 JSON。检查生成响应的代码。
  • 这是我的 API 的响应。
  • 您使用什么语言?

标签: javascript jquery json codeigniter twitter-bootstrap


【解决方案1】:
data = $.parseJSON(jsonAsTextFromServer);
$.each( data.message, function( key, value ) {
    $.each( value, function( err_key, err_value ) {
        alert( key + "::" + err_key + ": " + err_value );
    });

});

顺便说一句,此响应格式错误。它应该由 { }

包裹

DEMO (Updated)

【讨论】:

  • 实际上这看起来像: {"status":"error","message":{"amount":{"notFloat":"'asd' doesn't seem to be a float"} }}..
猜你喜欢
  • 2019-04-19
  • 1970-01-01
  • 2020-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多