【问题标题】:How to read my complex JSON Stringify data in JQuery?如何在 JQuery 中读取我复杂的 JSON Stringify 数据?
【发布时间】:2019-03-14 03:32:39
【问题描述】:

我正在尝试显示消息电子邮件“xxx@gmail.com”已被占用 从这个 JSON

{"readyState":4,"responseText":"{\"Message\":\"The request is invalid.\",\"ModelState\":{\"\":[\"Name xxx is already taken.\",\"Email 'xxx@gmail.com' is already taken.\"]}}","responseJSON":{"Message":"The request is invalid.","ModelState":{"":["Name xxx is already taken.","Email 'xxx@gmail.com' is already taken."]}},"status":400,"statusText":"Bad Request"}

我已经检查过我之前的问题link。我尝试了相同和不同的类型,如下所示。但我失败了

fail(function (showError) {
        console.log('Signup failed');
        console.log('full error = ' + JSON.stringify(showError));
        var response = JSON.stringify(showError);
        console.log("response error = "+JSON.stringify(response.responseText.ModelState));

        console.log('Message error = ' + JSON.parse(showError.responseText).Message);
        console.log('ModelState error = ' + JSON.parse(showError.responseText).ModelState[0]);
        //console.log('ModelState error val= ' + JSON.parse(showError.responseText).ModelState[0].val);
        console.log('ModelState error text= ' + JSON.parse(showError.responseText).ModelState[0].text);
    });

【问题讨论】:

  • 甚至不确定responseText 是否有效,但 chrome 会解析它。 ModelState 是一个带有空字符串作为键的对象 - 所以 JSON.parse(showError.responseText).ModelState[""][0]
  • @tymeJV 是的,它正在工作。请回答。我会接受的。

标签: jquery json jsonparser


【解决方案1】:

ModelState 是一个以空字符串作为第一个键的对象(其中包含您的错误数组) - 所以您可以这样做:

let errors = JSON.parse(showError.responseText).ModelState[""];

【讨论】:

    猜你喜欢
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多