2

I'm trying to parse some json data using Newtonsoft json.NET.

I can deserialize the data but when I try to use the deserialized data I get the error "'Newtonsoft.Json.Linq.JArray' does not contain a definition for 'leagueName'".

Here is the Json data I want to use.

[
{
    "queueType": "RANKED_SOLO_5x5",
    "hotStreak": false,
    "wins": 6,
    "veteran": false,
    "losses": 10,
    "playerOrTeamId": "39257940",
    "tier": "SILVER",
    "playerOrTeamName": "Kristmas Tree",
    "inactive": false,
    "rank": "V",
    "freshBlood": false,
    "leagueName": "Fizz's Gladiators",
    "leaguePoints": 47
},
{
    "queueType": "RANKED_FLEX_SR",
    "hotStreak": true,
    "wins": 100,
    "veteran": true,
    "losses": 99,
    "playerOrTeamId": "39257940",
    "tier": "SILVER",
    "playerOrTeamName": "Kristmas Tree",
    "inactive": false,
    "rank": "I",
    "freshBlood": false,
    "leagueName": "Xin Zhao's Heralds",
    "leaguePoints": 57
}
]

Here is my attempt at printing "hotStreak"

            var json = GetResponse("https://euw1.api.riotgames.com/lol/league/v3/positions/by-summoner/39257940?api_key=xxxx-xxxx-xxxx-xxxxxxxx");
            MessageBox.Show(json); // gives me the desired data

            var summonerInfo = JsonConvert.DeserializeObject<dynamic>(json);
            var leagueName = summonerInfo.leagueName;

            MessageBox.Show(leagueName);

How do I access the deserialized data?

  • 1
    Possible duplicate of [Deserialize JSON into C# dynamic object?](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) – Samvel Petrosov Jun 07 '17 at 11:03
  • 1
    You may refer this [answer](https://stackoverflow.com/a/42708158/4228458) if you want to deserialize into a strong type. – CodingYoshi Jun 07 '17 at 11:12

1 Answers1

1

First, note that this works: but, I don't recommend it:

var summonerInfo = JsonConvert.DeserializeObject<dynamic>(json);
foreach(var item in summonerInfo)
{
    Console.WriteLine(item.leagueName);
}

Instead, what I advise is:

First, define a type that matches the JSON:

class WhateverThisIs // rename me!
{
    public string QueueType { get; set; }
    public bool HotStreak { get; set; }
    public int Wins { get; set; }
    // ...
    public string LeagueName { get; set; }
    public int LeagueWins { get; set; }
}

Now deserialize as that type - in your case, into a list or array (the root object in the JSON is a JSON array ([...]), not a JSON object ({...})):

var summonerInfo = JsonConvert.DeserializeObject<List<WhateverThisIs>>(json);
foreach(var item in summonerInfo)
{
    Console.WriteLine(item.LeagueName);
}

You can do things using dynamic, but it is much more work than simply defining a type.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Works perfectly thanks! However I'm not sure I understand how it works. Is summonerInfo in this case a list of objects from the class (in this case I called it Summoner) made to match the JSON? –  Jun 07 '17 at 11:30
  • 1
    @FastCow which are you asking about? the first, or the second? The second is exactly as you say - a list of objects that match the shape of the JSON. The first uses the `dynamic` API of JSON.NET, which will *work*, but is *only* going to hurt you: it is much harder to get *right*, and it is much more expensive at runtime. – Marc Gravell Jun 07 '17 at 11:34