2

I need to deserialize a json array into a C# class using Newtonsoft.Json, but i dont know how to do it.

i have already tried using

public  LeagueInfo[] leagueinfo { get; set; }

and

public  List<LeagueInfo> leagueinfo { get; set; }

What i have now is: - Classes

public class League
{
    public  LeagueInfo[] leagueinfo { get; set; }
}

public class LeagueInfo
{
    public string queueType { get; set; }
    public string summonerName { get; set; }
    public bool hotStreak { get; set; }
    public int wins { get; set; }
    public bool veteran { get; set; }
    public int losses { get; set; }
    public string rank { get; set; }
    public string tier { get; set; }
    public bool inactive { get; set; }
    public bool freshBlood { get; set; }
    public string leagueId { get; set; }
    public string summonerId { get; set; }
    public int leaguePoints { get; set; }
}

Code

league = JsonConvert.DeserializeObject<League>(data);

When i try to do that i get this error:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'League' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.'

My JSON String is

[
    {
        "queueType": "RANKED_SOLO_5x5",
        "summonerName": "R2FTW",
        "hotStreak": false,
        "wins": 76,
        "veteran": true,
        "losses": 97,
        "rank": "II",
        "tier": "BRONZE",
        "inactive": false,
        "freshBlood": false,
        "leagueId": "2a74bbd0-20ba-11e9-9c10-d4ae52a70a5a",
        "summonerId": "n9Sw-4lZHg5Cd2oeMxe8dj9WGv1XQS3GZEMPX1VZHgVH5w",
        "leaguePoints": 75
    },
    {
        "queueType": "RANKED_FLEX_SR",
        "summonerName": "R2FTW",
        "hotStreak": false,
        "wins": 0,
        "veteran": false,
        "losses": 12,
        "rank": "IV",
        "tier": "IRON",
        "inactive": false,
        "freshBlood": false,
        "leagueId": "b75d4e60-2234-11e9-a815-d4ae527982aa",
        "summonerId": "n9Sw-4lZHg5Cd2oeMxe8dj9WGv1XQS3GZEMPX1VZHgVH5w",
        "leaguePoints": 0
    }
]

Sorry if this is a dumb question, i'm new to programing in general. How do i do that?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Arthur Mansur
  • 67
  • 1
  • 7
  • 2
    Possible duplicate of [Cannot deserialize the current JSON array (e.g. \[1,2,3\]) into type](https://stackoverflow.com/questions/17762032/cannot-deserialize-the-current-json-array-e-g-1-2-3-into-type) – Ňɏssa Pøngjǣrdenlarp Jul 23 '19 at 15:57
  • 2
    `JsonConvert.DeserializeObject(data);` should work. Are you saying it doesn't? –  Jul 23 '19 at 15:59

1 Answers1

1

Try this:

League league = new League()
{
    leagueinfo = JsonConvert.DeserializeObject<LeagueInfo[]>(data)
};
Mark Cilia Vincenti
  • 1,410
  • 8
  • 25