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.

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
TomTomGo
  • 41
  • 2
  • 5
  • Add a class Twopeas and inside it add a property twopeas it is type is a Summoner – Lamourou abdallah Jul 04 '14 at 23:37
  • Check out MY OWN answer for this post **http://stackoverflow.com/questions/25142325/getting-json-object-from-mvc-controller** I use MVC to help with this process. You will see how I call C# with AJAX, C# grabs the JSON, then I pass it back to Javascript to parse it. If you are newer to Javascript/AJAX I'd also recommend that you check out my **[Beginners introduction to Riot API and JSON, using Javascript and Ajax](https://developer.riotgames.com/discussion/riot-games-api/show/kvll5V8r)** :) – Austin Aug 06 '14 at 14:32

1 Answers1

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);
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300