简体   繁体   中英

Problems with deserializing Json from RIOT Api in C# UWP

I am new to stack overflow and I am a beginner programmer. I wanted to make an application that is about League of Legends but I have a problem. I just can't figure out how to deserialize the json into an object.

This is what I've tried

public class LOL
{
    public User user { get; set; }
}

public class User
{
    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; }
}

These are the classes I created using the website http://json2csharp.com/ .

I've just changed the name of these classes to something I like

This is my other class where I call the API

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

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

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

        return JsonConvert.DeserializeObject<LOL>(result);
    }
}

This is the place I use the API

private async void GetUserInfo_Click(object sender, RoutedEventArgs e)
{
     LOL user = await LOLFacade.ConnectToRiot("gigaxel");
     string name = user.user.name;
     int level = user.user.summonerLevel;

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

And I always get this error when I execute my code :( :

Object reference not set to an instance of an object.

Here is the Json:

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

Please if anyone can help me I would really appreciate it. I just started and I think that I made a lot of mistakes in this code but if you could help me I would really appreciate it :D

try the following class instead of your LOL class

public class LOL
{
   public User gigaxel { get; set; }
}

Your LOL model doesn't match with JSON that you received.

Look my answer in this question: Deserialize JSON into Object C#

The correct model is that

public class LOL
{
    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; }
}

Apart from the issues pointed by the other users, your error is due to the fact that you are trying to deserialize the JSON into an object that you never instantiated. You have to create the object before deserializing:

LOL LOLObj = new LOL { user = new User() };
return JsonConvert.DeserializeObject<LOLObj>(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM