如何在JQuery中读取我的复杂JSON Stringify数据?

时间:2019-03-13 19:17:12

标签: jquery json jsonparser

我正在尝试显示消息电子邮件“ 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);
    });

1 个答案:

答案 0 :(得分:1)

ModelState是一个以空字符串作为第一个键(包含错误数组)的对象-因此您可以执行以下操作:

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