Home > Net >  Get exception while deserializing JSON
Get exception while deserializing JSON

Time:01-09

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) 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!

CodePudding user response:

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

Demo @ .NET Fiddle

  • Related