嵌套的json读取

Nested json reading

本文关键字:读取 json 嵌套      更新时间:2024-03-31

我在读取以下json时遇到问题。

{
    "Message":"The request is invalid.",
    "ModelState":{
        "model.ConfirmPassword":["The password and confirmation password do not match.","The password and confirmation password do not match."]
    }
}

我正在尝试以下(结果不是空的,并且具有所有neded值)

function(result)
        {
        var test=result.responseText;
        var test1=test.Message;
        var test2=test[0];
        var test3=test["Message"];      
        }

第一个测试有所有json文本,但我只需要Message和稍后的其他文本请帮忙,因为我不知道我在阅读消息信息时犯了什么错误

您需要使用JSON.parse()将JSON数据转换为对象,然后才能在代码中访问它。所以这样的东西会起作用:

function(result)
    {
    var test=JSON.parse(result.responseText);
    var test1=test.Message;
    // Or you could use
    var test2=test["Message"];      
    }