0

I am fairly new to C# / JSON and am working on a pet project to show summoner/game information from league of legends.

I am trying to get the summoner id for the requested summoner name.

Here is the JSON returned:

{"twopeas": {
   "id": 42111241,
   "name": "Twopeas",
   "profileIconId": 549,
   "revisionDate": 1404482602000,
   "summonerLevel": 30
}}

Here is my summoner class:

public class Summoner
        {

            [JsonProperty(PropertyName = "id")]
            public string ID { get; set; }

        }

Here is the rest:

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());

    result = reader.ReadToEnd();

    var summonerInfo = JsonConvert.DeserializeObject<Summoner>(result);

    MessageBox.Show(summonerInfo.ID);

summonerInfo.ID is null and I don't know why.

I'm sure there is something glaringly obvious that I am missing, but I'm at a loss I can't for the life of me figure it out.

2

1 Answer 1

2

Your ID is null because your JSON doesn't match the class you're deserializing into. In the JSON, the id property is not at the top level: it is contained within an object which is the value of a top-level property called twopeas (presumably representing the summoner name). Since this property name can vary depending on your query, you should deserialize into a Dictionary<string, Summoner> like this:

var summoners = 
           JsonConvert.DeserializeObject<Dictionary<string, Summoner>>(result);

MessageBox.Show(summoners.Values.First().ID);
Sign up to request clarification or add additional context in comments.

1 Comment

This worked! Now I just need to figure out why...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.