英文:
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<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; }
}
Here's the JSON file:
{
    "total": 520738,
    "values": [
        {
            "name": "2",
            "count": 311326,
            "previous_count": 11224
        },
        {
            "name": "3",
            "count": 73749,
            "previous_count": 2747
        },
        {
            "name": "4",
            "count": 47810,
            "previous_count": 1830
        },
        {
            "name": "6",
            "count": 20414,
            "previous_count": 759
        },
        {
            "name": "5",
            "count": 14481,
            "previous_count": 596
        },
        {
            "name": "8",
            "count": 7258,
            "previous_count": 233
        },
        {
            "name": "7",
            "count": 6452,
            "previous_count": 286
        },
        {
            "name": "9",
            "count": 4439,
            "previous_count": 167
        },
        {
            "name": "10",
            "count": 4273,
            "previous_count": 145
        },
        {
            "name": "12",
            "count": 2969,
            "previous_count": 82
        },
        {
            "name": "11",
            "count": 1890,
            "previous_count": 65
        },
        {
            "name": "15",
            "count": 1279,
            "previous_count": 69
        },
        {
            "name": "16",
            "count": 1109,
            "previous_count": 36
        },
        {
            "name": "1",
            "count": 1021,
            "previous_count": 448
        },
        {
            "name": "20",
            "count": 906,
            "previous_count": 68
        },
        {
            "name": "18",
            "count": 867,
            "previous_count": 14
        },
        {
            "name": "14",
            "count": 844,
            "previous_count": 58
        },
        {
            "name": "13",
            "count": 380,
            "previous_count": 47
        },
        {
            "name": "30",
            "count": 376,
            "previous_count": 9
        },
        {
            "name": "40",
            "count": 296,
            "previous_count": 8
        },
        {
            "name": "25",
            "count": 282,
            "previous_count": 2
        },
        {
            "name": "17",
            "count": 224,
            "previous_count": 14
        },
        {
            "name": "24",
            "count": 188,
            "previous_count": 23
        },
        {
            "name": "23",
            "count": 165,
            "previous_count": 1
        },
        {
            "name": "32",
            "count": 157,
            "previous_count": 0
        },
        {
            "name": "28",
            "count": 71,
            "previous_count": 8
        },
        {
            "name": "60",
            "count": 48,
            "previous_count": 2
        },
        {
            "name": "39",
            "count": 39,
            "previous_count": 2
        },
        {
            "name": "46",
            "count": 29,
            "previous_count": 0
        },
        {
            "name": "22",
            "count": 27,
            "previous_count": 5
        },
        {
            "name": "90",
            "count": 24,
            "previous_count": 0
        },
        {
            "name": "21",
            "count": 23,
            "previous_count": 11
        },
        {
            "name": "19",
            "count": 20,
            "previous_count": 0
        },
        {
            "name": "26",
            "count": 17,
            "previous_count": 1
        },
        {
            "name": "41",
            "count": 16,
            "previous_count": 0
        },
        {
            "name": "33",
            "count": 13,
            "previous_count": 0
        },
        {
            "name": "34",
            "count": 10,
            "previous_count": 4
        },
        {
            "name": "35",
            "count": 10,
            "previous_count": 0
        },
        {
            "name": "27",
            "count": 9,
            "previous_count": 4
        },
        {
            "name": "43",
            "count": 8,
            "previous_count": 0
        },
        {
            "name": "38",
            "count": 8,
            "previous_count": 0
        },
        {
            "name": "81",
            "count": 7,
            "previous_count": 0
        },
        {
            "name": "61",
            "count": 6,
            "previous_count": 0
        },
        {
            "name": "76",
            "count": 3,
            "previous_count": 0
        },
        {
            "name": "48",
            "count": 3,
            "previous_count": 3
        },
        {
            "name": "140",
            "count": 2,
            "previous_count": 2
        },
        {
            "name": "51",
            "count": 2,
            "previous_count": 0
        },
        {
            "name": "55",
            "count": 2,
            "previous_count": 0
        },
        {
            "name": "162",
            "count": 1,
            "previous_count": 0
        },
        {
            "name": "120",
            "count": 1,
            "previous_count": 0
        },
        {
            "name": "170",
            "count": 1,
            "previous_count": 1
        },
        {
            "name": "145",
            "count": 1,
            "previous_count": 0
        },
        {
            "name": "160",
            "count": 1,
            "previous_count": 0
        },
        {
            "name": "29",
            "count": 1,
            "previous_count": 3
        },
        {
            "name": "104",
            "count": 1,
            "previous_count": 0
        },
        {
            "name": "235",
            "count": 1,
            "previous_count": 0
        },
        {
            "name": "110",
            "count": 1,
            "previous_count": 0
        },
        {
            "name": "42",
            "count": 1,
            "previous_count": 15
        },
        {
            "name": "245",
            "count": 1,
            "previous_count": 0
        },
        {
            "name": "275",
            "count": 1,
            "previous_count": 0
        },
        {
            "name": "288",
            "count": 1,
            "previous_count": 0
        },
        {
            "name": "Other",
            "count": 17173,
            "previous_count": 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()
	});
英文:
Your attached JSON is an object with Rootobject type, but not an array.
Deserialize as Rootobject.
Rootobject item = JsonConvert.DeserializeObject<Rootobject>(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("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; }
}
Or With CamelCasePropertyNamesContractResolver
Rootobject item = JsonConvert.DeserializeObject<Rootobject>(json, new JsonSerializerSettings
	{
		ContractResolver = new CamelCasePropertyNamesContractResolver()
	});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论