问题描述
请帮助我.我在哪里缺少信息?我需要反序列化以下 JSON 字符串.
Please help me. Where I am missing info? I need to deserialize the following JSON string.
{"results":[{"series":[{"name":"PWR_00000555","columns":["time","last"],"values":[["1970-01-01T00:00:00Z",72]]}]}]}
{"results":[{"series":[{"name":"PWR_00000555","columns":["time","last"],"values":[["1970-01-01T00:00:00Z",72]]}]}]}
为此,我定义了我的类:
For this, I have defined my class:
public class Serie
{
public Serie()
{
this.Points = new List<object[]>();
}
[JsonProperty(PropertyName = "results")]
public string results { get; set; }
[JsonProperty(PropertyName = "series")]
public string sries { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "columns")]
public string[] ColumnNames { get; set; }
[JsonProperty(PropertyName = "values")]
public List<object[]> Points { get; set; }
但是当我尝试使用反序列化器时,它给出了一个异常.
But when I try using the de-serializer, it gives an exception.
{"无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[InfluxDB.Serie]',因为该类型需要要正确反序列化的 JSON 数组(例如 [1,2,3]).
要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或将反序列化类型更改为可以从 JSON 对象反序列化的普通 .NET 类型(例如,不是整数之类的原始类型,不是数组或 List
{"Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[InfluxDB.Serie]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'results', line 2, position 12."}
推荐答案
您收到此错误是因为您的 JSON 是分层的,而您的类本质上是扁平的.如果您使用 JSONLint.com 来验证和重新格式化 JSON,您可以更好地看到结构:
You are getting this error because your JSON is hierarchical while your class is essentially flat. If you use JSONLint.com to validate and reformat the JSON, you can see the structure better:
{
"results": [
{
"series": [
{
"name": "PWR_00000555",
"columns": [
"time",
"last"
],
"values": [
[
"1970-01-01T00:00:00Z",
72
]
]
}
]
}
]
}
这对应于以下类结构(我最初使用 json2csharp.com 生成,然后手动编辑添加[JsonProperty]
属性):
This corresponds to the following class structure (which I initially generated using json2csharp.com, then manually edited to add the [JsonProperty]
attributes):
public class RootObject
{
[JsonProperty("results")]
public List<Result> Results { get; set; }
}
public class Result
{
[JsonProperty("series")]
public List<Series> Series { get; set; }
}
public class Series
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("columns")]
public List<string> ColumnNames { get; set; }
[JsonProperty("values")]
public List<List<object>> Points { get; set; }
}
您可以像这样将您的 JSON 反序列化为上述类结构:
You can deserialize your JSON into the above class structure like this:
var root = JsonConvert.DeserializeObject<RootObject>(jsonString);
小提琴:https://dotnetfiddle.net/50Z64s
这篇关于得到“因为该类型需要一个 JSON 数组(例如 [1,2,3])才能正确反序列化"反序列化 Json 对象时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!