遇到在反序列化 JSON 时出现异常。

huangapple go评论74阅读模式
英文:

Get exception while deserializing JSON

问题

我遇到了一个问题,无法将JSON文件反序列化为对象,它返回了一个JSON异常,以下是我的代码:

class Program
{
    static void Main(string[] args)
    {
        LoadJson(filePath);
    }

    public static void LoadJson(string filePath)
    {
        using (StreamReader r = new StreamReader(filePath))
        {
            string json = r.ReadToEnd();
            List<Rootobject> items = JsonConvert.DeserializeObject<List<Rootobject>>(json);
        }
    }
}

public class Rootobject
{
    public int total { get; set; }
    public Value[] values { get; set; }
}

public class Value
{
    public string name { get; set; }
    public int count { get; set; }
    public int previous_count { get; set; }
}

这是JSON文件:

{
    "total": 520738,
    "values": [
        {
            "name": "2",
            "count": 311326,
            "previous_count": 11224
        },
        // 其他值...
        {
            "name": "Other",
            "count": 17173,
            "previous_count": 934
        }
    ]
}

尝试将Value类更改为List,但仍然遇到相同的问题。这是返回的异常:

要修复此错误,要么将JSON更改为JSON数组(例如[1,2,3]),要么更改反序列化类型,使其成为普通的.NET类型(例如不是整数这样的基本类型,不是数组或List这样的集合类型),可以添加JsonObjectAttribute到类型中,以强制从JSON对象反序列化。
路径 'total',行 2,位置 12。

谢谢!

英文:

I'm having a problem deserializing a JSON file to an object, it's returning a JSON exception, here's my code:

class Program
{
    static void Main(string[] args)
    {
        LoadJson(filePath);
    }

    public static void LoadJson(string filePath)
    {
        using (StreamReader r = new StreamReader(filePath))
        {
            string json = r.ReadToEnd();
            List&lt;Rootobject&gt; items = JsonConvert.DeserializeObject&lt;List&lt;Rootobject&gt;&gt;(json);
        }
    }
}

public class Rootobject
{
    public int total { get; set; }
    public Value[] values { get; set; }
}

public class Value
{
    public string name { get; set; }
    public int count { get; set; }
    public int previous_count { get; set; }
}

Here's the JSON file:

{
    &quot;total&quot;: 520738,
    &quot;values&quot;: [
        {
            &quot;name&quot;: &quot;2&quot;,
            &quot;count&quot;: 311326,
            &quot;previous_count&quot;: 11224
        },
        {
            &quot;name&quot;: &quot;3&quot;,
            &quot;count&quot;: 73749,
            &quot;previous_count&quot;: 2747
        },
        {
            &quot;name&quot;: &quot;4&quot;,
            &quot;count&quot;: 47810,
            &quot;previous_count&quot;: 1830
        },
        {
            &quot;name&quot;: &quot;6&quot;,
            &quot;count&quot;: 20414,
            &quot;previous_count&quot;: 759
        },
        {
            &quot;name&quot;: &quot;5&quot;,
            &quot;count&quot;: 14481,
            &quot;previous_count&quot;: 596
        },
        {
            &quot;name&quot;: &quot;8&quot;,
            &quot;count&quot;: 7258,
            &quot;previous_count&quot;: 233
        },
        {
            &quot;name&quot;: &quot;7&quot;,
            &quot;count&quot;: 6452,
            &quot;previous_count&quot;: 286
        },
        {
            &quot;name&quot;: &quot;9&quot;,
            &quot;count&quot;: 4439,
            &quot;previous_count&quot;: 167
        },
        {
            &quot;name&quot;: &quot;10&quot;,
            &quot;count&quot;: 4273,
            &quot;previous_count&quot;: 145
        },
        {
            &quot;name&quot;: &quot;12&quot;,
            &quot;count&quot;: 2969,
            &quot;previous_count&quot;: 82
        },
        {
            &quot;name&quot;: &quot;11&quot;,
            &quot;count&quot;: 1890,
            &quot;previous_count&quot;: 65
        },
        {
            &quot;name&quot;: &quot;15&quot;,
            &quot;count&quot;: 1279,
            &quot;previous_count&quot;: 69
        },
        {
            &quot;name&quot;: &quot;16&quot;,
            &quot;count&quot;: 1109,
            &quot;previous_count&quot;: 36
        },
        {
            &quot;name&quot;: &quot;1&quot;,
            &quot;count&quot;: 1021,
            &quot;previous_count&quot;: 448
        },
        {
            &quot;name&quot;: &quot;20&quot;,
            &quot;count&quot;: 906,
            &quot;previous_count&quot;: 68
        },
        {
            &quot;name&quot;: &quot;18&quot;,
            &quot;count&quot;: 867,
            &quot;previous_count&quot;: 14
        },
        {
            &quot;name&quot;: &quot;14&quot;,
            &quot;count&quot;: 844,
            &quot;previous_count&quot;: 58
        },
        {
            &quot;name&quot;: &quot;13&quot;,
            &quot;count&quot;: 380,
            &quot;previous_count&quot;: 47
        },
        {
            &quot;name&quot;: &quot;30&quot;,
            &quot;count&quot;: 376,
            &quot;previous_count&quot;: 9
        },
        {
            &quot;name&quot;: &quot;40&quot;,
            &quot;count&quot;: 296,
            &quot;previous_count&quot;: 8
        },
        {
            &quot;name&quot;: &quot;25&quot;,
            &quot;count&quot;: 282,
            &quot;previous_count&quot;: 2
        },
        {
            &quot;name&quot;: &quot;17&quot;,
            &quot;count&quot;: 224,
            &quot;previous_count&quot;: 14
        },
        {
            &quot;name&quot;: &quot;24&quot;,
            &quot;count&quot;: 188,
            &quot;previous_count&quot;: 23
        },
        {
            &quot;name&quot;: &quot;23&quot;,
            &quot;count&quot;: 165,
            &quot;previous_count&quot;: 1
        },
        {
            &quot;name&quot;: &quot;32&quot;,
            &quot;count&quot;: 157,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;28&quot;,
            &quot;count&quot;: 71,
            &quot;previous_count&quot;: 8
        },
        {
            &quot;name&quot;: &quot;60&quot;,
            &quot;count&quot;: 48,
            &quot;previous_count&quot;: 2
        },
        {
            &quot;name&quot;: &quot;39&quot;,
            &quot;count&quot;: 39,
            &quot;previous_count&quot;: 2
        },
        {
            &quot;name&quot;: &quot;46&quot;,
            &quot;count&quot;: 29,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;22&quot;,
            &quot;count&quot;: 27,
            &quot;previous_count&quot;: 5
        },
        {
            &quot;name&quot;: &quot;90&quot;,
            &quot;count&quot;: 24,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;21&quot;,
            &quot;count&quot;: 23,
            &quot;previous_count&quot;: 11
        },
        {
            &quot;name&quot;: &quot;19&quot;,
            &quot;count&quot;: 20,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;26&quot;,
            &quot;count&quot;: 17,
            &quot;previous_count&quot;: 1
        },
        {
            &quot;name&quot;: &quot;41&quot;,
            &quot;count&quot;: 16,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;33&quot;,
            &quot;count&quot;: 13,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;34&quot;,
            &quot;count&quot;: 10,
            &quot;previous_count&quot;: 4
        },
        {
            &quot;name&quot;: &quot;35&quot;,
            &quot;count&quot;: 10,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;27&quot;,
            &quot;count&quot;: 9,
            &quot;previous_count&quot;: 4
        },
        {
            &quot;name&quot;: &quot;43&quot;,
            &quot;count&quot;: 8,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;38&quot;,
            &quot;count&quot;: 8,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;81&quot;,
            &quot;count&quot;: 7,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;61&quot;,
            &quot;count&quot;: 6,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;76&quot;,
            &quot;count&quot;: 3,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;48&quot;,
            &quot;count&quot;: 3,
            &quot;previous_count&quot;: 3
        },
        {
            &quot;name&quot;: &quot;140&quot;,
            &quot;count&quot;: 2,
            &quot;previous_count&quot;: 2
        },
        {
            &quot;name&quot;: &quot;51&quot;,
            &quot;count&quot;: 2,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;55&quot;,
            &quot;count&quot;: 2,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;162&quot;,
            &quot;count&quot;: 1,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;120&quot;,
            &quot;count&quot;: 1,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;170&quot;,
            &quot;count&quot;: 1,
            &quot;previous_count&quot;: 1
        },
        {
            &quot;name&quot;: &quot;145&quot;,
            &quot;count&quot;: 1,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;160&quot;,
            &quot;count&quot;: 1,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;29&quot;,
            &quot;count&quot;: 1,
            &quot;previous_count&quot;: 3
        },
        {
            &quot;name&quot;: &quot;104&quot;,
            &quot;count&quot;: 1,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;235&quot;,
            &quot;count&quot;: 1,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;110&quot;,
            &quot;count&quot;: 1,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;42&quot;,
            &quot;count&quot;: 1,
            &quot;previous_count&quot;: 15
        },
        {
            &quot;name&quot;: &quot;245&quot;,
            &quot;count&quot;: 1,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;275&quot;,
            &quot;count&quot;: 1,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;288&quot;,
            &quot;count&quot;: 1,
            &quot;previous_count&quot;: 0
        },
        {
            &quot;name&quot;: &quot;Other&quot;,
            &quot;count&quot;: 17173,
            &quot;previous_count&quot;: 934
        }
    ]
}

Tried changing the Value class to a List but still had the same issue. This is the returned exception:

> 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 'total', line 2, position 12.

Thanks!

答案1

得分: 3

你附加的JSON是一个具有Rootobject类型的对象,而不是一个数组。

将其反序列化为Rootobject

Rootobject item = JsonConvert.DeserializeObject<Rootobject>(json);

在命名约定方面,我们建议对public成员使用帕斯卡命名法。

将所有属性修改为帕斯卡命名法,并应用[JsonProperty]属性以映射JSON中的属性名称。

使用[JsonProperty]属性

public class Rootobject
{
    [JsonProperty("total")]
    public int Total { get; set; }

    [JsonProperty("values")]
    public Value[] Values { get; set; }
}

public class Value
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("count")]
    public int Count { get; set; }

    [JsonProperty("previous_count")]
    public int PreviousCount { get; set; }
}

或者使用CamelCasePropertyNamesContractResolver

Rootobject item = JsonConvert.DeserializeObject<Rootobject>(json, new JsonSerializerSettings
	{
		ContractResolver = new CamelCasePropertyNamesContractResolver()
	});

在.NET Fiddle上演示

英文:

Your attached JSON is an object with Rootobject type, but not an array.

Deserialize as Rootobject.

Rootobject item = JsonConvert.DeserializeObject&lt;Rootobject&gt;(json);

While in naming conventions, we recommend using Pascal Casing for public members.

Modify all your properties to Pascal Case and apply the [JsonProperty] attribute to map the name of properties in JSON.

With [JsonProperty] attribute

public class Rootobject
{
    [JsonProperty(&quot;total&quot;)]
    public int Total { get; set; }

    [JsonProperty(&quot;values&quot;)]
    public Value[] Values { get; set; }
}

public class Value
{
    [JsonProperty(&quot;name&quot;)]
    public string Name { get; set; }

    [JsonProperty(&quot;count&quot;)]
    public int Count { get; set; }

    [JsonProperty(&quot;previous_count&quot;)]
    public int PreviousCount { get; set; }
}

Or With CamelCasePropertyNamesContractResolver

Rootobject item = JsonConvert.DeserializeObject&lt;Rootobject&gt;(json, new JsonSerializerSettings
	{
		ContractResolver = new CamelCasePropertyNamesContractResolver()
	});

Demo @ .NET Fiddle

huangapple
  • 本文由 发表于 2023年1月9日 10:30:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定