代码之家  ›  专栏  ›  技术社区  ›  Liam neesan

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

  •  0
  • Liam neesan  · 技术社区  · 5 年前

    我正在显示消息 电子邮件“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 回复  |  直到 5 年前
        1
  •  1
  •   tymeJV    5 年前

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

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