Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize json into c# classes

Tags:

json

c#

I am trying to make a UWP app using the RIOT API for League of Legends.

When I go to their website to generate the JSON I get something like this:

{"gigaxel": {
  "id": 36588106,
   "name": "Gigaxel",
   "profileIconId": 713,
   "revisionDate": 1451577643000,
   "summonerLevel": 30
}}

When I select this JSON and copy it into a new class using the special paste method in Visual Studio 2015 I get these classes with these properties:

public class Rootobject
{
    public Gigaxel gigaxel { get; set; }
}

public class Gigaxel
{
    public int id { get; set; }
    public string name { get; set; }
    public int profileIconId { get; set; }
    public long revisionDate { get; set; }
    public int summonerLevel { get; set; }
}

I created a new class called LOLFacade for connecting to the RiotAPI:

 public class LOLFacade
{
    private const string APIKey = "secret :D";

    public async static Task<Rootobject> ConnectToRiot(string user,string regionName)
    {
        var http = new HttpClient();
        string riotURL = String.Format("https://{0}.api.pvp.net/api/lol/{0}/v1.4/summoner/by-name/{1}?api_key={2}",regionName, user, APIKey);
        var response = await http.GetAsync(riotURL);

        var result = await response.Content.ReadAsStringAsync();

        return JsonConvert.DeserializeObject<Rootobject>(result);

    }

}

This is the button event handler method:

        Rootobject root = new Rootobject { gigaxel = new Gigaxel() };
        root = await LOLFacade.ConnectToRiot("gigaxel","EUNE");
        string name = root.gigaxel.name;
        int level = root.gigaxel.summonerLevel;

        InfoTextBlock.Text = name + " is level " + level;

I hard coded the regionName and the user for testing purposes. This works with my username: "gigaxel".
When I try another username for example like "xenon94" I get an exception:

Object reference not set to an instance of an object.

When I change the property name in Rootobject from gigaxel to xenon94 like this:

public class Rootobject
{
    public Gigaxel xenon94 { get; set; }
}

When I recompile my code it works for the username xenon94 but it doesn't work for my username "gigaxel".
I want it to work for any given username.

like image 737
Gigaxel Avatar asked Jan 01 '16 12:01

Gigaxel


1 Answers

The problem is that the json object has a property that is named gigaxel. You will have to retrieve the inside object, like so:

var json = JsonConvert.DeserializeObject<JObject>(x).First.First;

From there, you can get the name and other things by the indexer:

string name = (string)json["name"];
int summonerlevel = (int)json["summonerLevel"]

To elaborate, the JsonConvert.DeserializeObject(x) will return a new JObject which has only one object. Hence the First call. Moreover, this object has only one property, named "gigaxel". The value of this property is the information we require, e.g. name. We want to retrieve this information regardless of what the property is named. Therefore we call First again to retrieve the value of this property.

like image 193
lenkan Avatar answered Nov 12 '22 20:11

lenkan