JSON - 锯齿状数组反序列化

JSON - Jagged Array Deserialization

我有一个问题,这可能是初学者的问题,但我对 JSON 不是很熟悉。然而,我正在尝试使用 C# Newtonsoft.Json 反序列化下面的 JSON,然而,它一直失败,任何人都可以建议如何反序列化它以及如何在 C# 中建模它?

[
    {
        "fields": [
            [
                "name",
                "value",
                "values",
                "error"
            ],
            [
                "correspondsApi",
                "N",
                null,
                ""
            ],
            [
                "username",
                "test@test.com",
                null,
                ""
            ],
            [
                "password",
                "",
                null,
                ""
            ],
            [
                "accountid",
                "",
                null,
                ""
            ],
            [
                "rememberMe",
                "Y",
                null,
                ""
            ],
            [
                "language",
                "en-US",
                null,
                ""
            ],
            [
                "S",
                "tokenValue",
                null,
                null
            ],
            [
                "authToken",
                "",
                null,
                ""
            ]
        ],
        "success": "Y",
        "message": "Access is granted"
    }
]

下面是我试过的不断抛出异常的代码: 型号:

public class MyArray
    {
        public List<List<string>> fields { get; set; }
        public string success { get; set; }
        public string message { get; set; }
    }

    public class Root
    {
        public List<MyArray> MyArray { get; set; }
    }

代码:

using Newtonsoft.Json;

static void Main(string[] args)
{     
   string jsonContent = "[{\"fields\":[[\"name\",\"value\",\"values\",\"error\"],[\"correspondsApi\",\"N\",null,\"\"],[\"username\",\"test@test.com\",null,\"\"],[\"password\",\"\",null,\"\"],[\"accountid\",\"\",null,\"\"],[\"rememberMe\",\"Y\",null,\"\"],[\"language\",\"en-US\",null,\"\"],[\"S\",\"tokenValue\",null,null],[\"authToken\",\"\",null,\"\"]], \"success\":\"Y\",\"message\":\"Access is granted\"}]";
            JsonConvert.DeserializeObject<Root>(jsonContent);
            
}     

异常:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'LearningOnly.Root' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.'

我运行你的json在 https://json2csharp.com/

回答您的问题:“如何在 C# 中建模?”

public class MyArray    {
    public List<List<string>> fields { get; set; } 
    public string success { get; set; } 
    public string message { get; set; } 
}

public class Root    {
    public List<MyArray> MyArray { get; set; } 
}

您的 json 和对象不匹配。要使用 Root,您需要像 {MyArray: [...]} 一样的 json,即具有包含数组的 MyArray 属性 的对象。但你没有。您实际上想使用:

JsonConvert.DeserializeObject<List<MyArray>>(jsonContent);

因为你的根 json 结构是一个数组,即 [{},{}] 不是一个对象。所以你需要把它变成一个枚举器(List<>或数组等)

这里的工作示例:https://dotnetfiddle.net/zA4VLA