简体   繁体   中英

JSON - Jagged Array Deserialization

I do have a question, it maybe a beginner's question but am not really very familiar with JSON. however, am trying to de-serialize the below JSON using C# Newtonsoft.Json, however, it keeps failing, can anyone please advise how to de-serialize it & how it has to be modeled in 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"
    }
]

Below is the code that I have tried which keeps throwing an exception: MODEL:

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; }
    }

CODE:

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);
            
}     

EXCEPTION:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (eg [1,2,3]) into type 'LearningOnly.Root' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg 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.'

Your json and objects don't match. To use Root you would need json like {MyArray: [...]} , ie an object with a MyArray property containing an array. But you don't. You actually want to use:

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

Because your root json structure is an array, ie [{},{}] not an object. so you need to turn it into an enumerator ( List<> or array, etc.)

Working example here: https://dotnetfiddle.net/zA4VLA

I ran your json in https://json2csharp.com/

Answer to your question: "how it has to be modeled in 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; } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM