获得“因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化"反序列化Json对象时发生错误 [英] Getting "because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly" error when deserializing a Json object

查看:291
本文介绍了获得“因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化"反序列化Json对象时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我.我在哪里缺少信息? 我需要反序列化以下JSON字符串.

Please help me. Where I am missing info? I need to deserialize the following JSON string.

{结果":[{系列":[{名称":"PWR_00000555",列":[时间",最后"],值":[["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])正确反序列化.\ r \ n要解决此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其正确可以从JSON对象反序列化的普通.NET类型(例如,不是整数之类的原始类型,不是数组或List< T>之类的集合类型);还可以将JsonObjectAttribute添加到该类型中,以强制其从JSON对象反序列化. JSON对象.\ r \ nPath'results',第2行,位置12.}

{"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.\r\nTo 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.\r\nPath '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对象时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆