[英]JSON - Jagged Array Deserialization
I do have a question, it maybe a beginner's question but am not really very familiar with JSON.我确实有一个问题,这可能是初学者的问题,但我对 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#?
但是,我正在尝试使用 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"
}
]
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.
Newtonsoft.Json.JsonSerializationException: '无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 'LearningOnly.Root',因为该类型需要一个 JSON 对象(例如 {"name":"value"})才能正确反序列化。 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.
要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList),例如可以从 JSON 数组反序列化。 JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
JsonArrayAttribute 也可以添加到类型以强制它从 JSON 数组反序列化。 Path '', line 1, position 1.'
路径 '',第 1 行,位置 1。'
Your json and objects don't match.您的 json 和对象不匹配。 To use
Root
you would need json like {MyArray: [...]}
, ie an object with a MyArray
property containing an array.要使用
Root
您需要像{MyArray: [...]}
这样的 json,即具有包含数组的MyArray
属性的对象。 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.因为你的根 json 结构是一个数组,即
[{},{}]
不是一个对象。 so you need to turn it into an enumerator ( List<>
or array, etc.)所以你需要把它变成一个枚举器(
List<>
或 array 等)
Working example here: https://dotnetfiddle.net/zA4VLA这里的工作示例: https : //dotnetfiddle.net/zA4VLA
I ran your json in https://json2csharp.com/我在https://json2csharp.com/ 中运行了你的 json
Answer to your question: "how it has to be modeled in C#?"回答您的问题:“它必须如何在 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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.