反序列化JSON时出现异常

omqzjyyz  于 2023-01-10  发布在  其他
关注(0)|答案(1)|浏览(177)

我在将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": "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
        }
    ]
}

尝试将Value类更改为List,但仍存在相同问题。这是返回的异常:
要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通.NET类型(例如,不是像整数这样的基元类型,也不是像数组或列表这样的集合类型)。还可以将JsonObjectAttribute添加到该类型以强制其从JSON对象反序列化。路径“total”,2号线12号位置。
谢谢!

qlfbtfca

qlfbtfca1#

附加的JSON是Rootobject类型的对象,但不是数组。
反序列化为Rootobject

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

在命名约定中,我们建议对public成员使用Pascal大小写。
将所有属性修改为PascalCase,并应用[JsonProperty]属性来MapJSON中的属性名称。

具有[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()
    });

Demo @ .NET Fiddle

相关问题