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 (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.'