-1

I am getting nulls deserializing object. Tried to remove that List keyword, but then I am getting nulls. Error-

Cannot deserialize the current JSON object

`

Maybe you have any ideas how to fix that. :)

Code:

            client.BaseAddress = new Uri("https://euw1.api.riotgames.com/lol/match/v4/matchlists/by-account/");
            string s = client.GetStringAsync("SRAUZPYTqglRgTjMEzaqY1s-wFMaNZnCjgBHMqQNDnJeJNw?endIndex=10&api_key=RGAPI-7bc6b22c-3ce3-41e6-bfbd-90b1eccb212f").Result;
            var rankInfoList = JsonConvert.DeserializeObject<List<MatchInfo>>(s);

Model:

public class MatchInfo
    {
       
        public string gameId { get; set; }
        public string champion { get; set; }
    }

Json:

{
    "matches": [
        {
            "platformId": "EUW1",
            "gameId": 4961339963,
            "champion": 1,
            "queue": 420,
            "season": 13,
            "timestamp": 1607031715226,
            "role": "SOLO",
            "lane": "MID"
        },
        {
            "platformId": "EUW1",
            "gameId": 4961185949,
            "champion": 238,
            "queue": 420,
            "season": 13,
            "timestamp": 1607026682284,
            "role": "SOLO",
            "lane": "MID"
        }
    ],
    "startIndex": 0,
    "endIndex": 2,
    "totalGames": 120
}

3 Answers3

1

You need to create a RootObject that would have matches as a property. matches would then be a List of objects (MatchInfo)

Your RootObject will look like this,

public class RootObject
{
    public List<MatchInfo> matches { get; set; }
    public int startIndex { get; set; }
    public int endIndex { get; set; }
    public int totalGames { get; set; }
}

public class MatchInfo
{
    public string platformId { get; set; }
    public long gameId { get; set; }
    public int champion { get; set; }
    public int queue { get; set; }
    public int season { get; set; }
    public long timestamp { get; set; }
    public string role { get; set; }
    public string lane { get; set; }
}

You will then deserialize using the following statement

var rankInfoList = JsonConvert.DeserializeObject<RootObject>(s);
Jawad
  • 11,028
  • 3
  • 24
  • 37
0

Because the JSON doesn't match the model. The model you're trying to deserialize it to is a List<MatchInfo>, but the JSON is not a list, it's an object with a property that contains a list. Create a containing model:

public class MatchInfos
{
    public List<MatchInfo> matches { get; set; }
}

And deserialize into that:

var rankInfoList = JsonConvert.DeserializeObject<MatchInfos>(s);
David
  • 208,112
  • 36
  • 198
  • 279
0

matches is a collection (MatchInfo) but you are treating the root as a colection. Instead try it like this:

void Main()
{
    string json = @"{
        ""matches"": [
            {
                ""platformId"": ""EUW1"",
                ""gameId"": 4961339963,
                ""champion"": 1,
                ""queue"": 420,
                ""season"": 13,
                ""timestamp"": 1607031715226,
                ""role"": ""SOLO"",
                ""lane"": ""MID""
            },
            {
                ""platformId"": ""EUW1"",
                ""gameId"": 4961185949,
                ""champion"": 238,
                ""queue"": 420,
                ""season"": 13,
                ""timestamp"": 1607026682284,
                ""role"": ""SOLO"",
                ""lane"": ""MID""
            }
        ],
        ""startIndex"": 0,
        ""endIndex"": 2,
        ""totalGames"": 120
    }";


    var data = JsonConvert.DeserializeAnonymousType(json, new { matches = new[] { new MatchInfo {}}});
    // data.matches is your MatchInfo list.
}

public class MatchInfo
{
    public string gameId { get; set; }
    public string champion { get; set; }
}
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39