0

I am trying deserialize riot champion API http://ddragon.leagueoflegends.com/cdn/12.23.1/data/en_US/champion.json

but I am still getting null in my championDTO...

Please help me...

//this is in main page and Constants.chamAPI is the link above with the string type

championDTO = await restService.GetChampData(Constants.champAPi);
 
// this is for Deserialize the JSON file 
public async Task RootChampionDTO GetChampData(string query) {
            
    RootChampionDTO championDTO = null;
    try
    {
        var response = await _client.GetAsync(query);
        if (response.IsSuccessStatusCode)
        {
            // var content = await response.Content.ReadAsStringAsync();
            championDTO = JsonConvert.DeserializeObject<RootChampionDTO>(query);
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine("\t\tERROR {0}", ex.Message);
    }

    return championDTO;
}

// this is the class for storing the data from json file.
 
namespace FinalProject
{
    public class RootChampionDTO {
        public List<Champion> Data { get; set; }
    }
    
    public class Champion
    {
        [JsonProperty("id")]
        public int Id { get; set; }
        [JsonProperty("key")]
        public string Key { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("title")]
        public string Title { get; set; }
    }
}

I tried Dictionary<string, Champion> data {get; set;} and this is still not working.

Stefan Wuebbe
  • 2,109
  • 5
  • 17
  • 28
Sean Moon
  • 1
  • 3
  • Please add code and data as text ([using code formatting](/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](//meta.stackoverflow.com/a/285557). Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. – gunr2171 Dec 09 '22 at 04:41
  • 2
    Use json2csharp.com to build a model for your json. At a quick glance it doesn’t appear that your current model matches the json – Jason Dec 09 '22 at 04:54
  • // Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse); public class Aatrox { public string version { get; set; } public string id { get; set; } public string key { get; set; } public string name { get; set; } public string title { get; set; } public string blurb { get; set; } public Info info { get; set; } public Image image { get; set; } public List tags { get; set; } ... } .... I get this but this has too much information – Sean Moon Dec 09 '22 at 05:10
  • 1
    @Jason has the correct answer. I converted your source json, it contains a series of disparate objects that do not match your model. You may want to modify your json before deserializing: you could extract the json object(s) you care about and ignore the others, for example. – Serialize Dec 09 '22 at 05:13
  • @Serialize is that mean my RootChampionDTO is not in correct form ? – Sean Moon Dec 09 '22 at 05:18
  • Yes, it does not match the actual data. If by “too much information” you mean it includes properties you don’t need, that’s ok. You can just ignore them – Jason Dec 09 '22 at 05:19
  • Take a look at the shape of your json data here: https://codebeautify.org/jsonviewer/y2265745c You can see that you have some fields and a large collection. You can, for example, take only the 'data' collection from that tree. – Serialize Dec 09 '22 at 05:32
  • It looks like data is a dictionary with the name of the champions as key and your dto as value. – Caveman74 Dec 09 '22 at 08:28

1 Answers1

0

if you need only data , you have to parse the json and deserialize data. You can use Dictionary

Dictionary<string,Champion> champions = JObject.Parse(json)["data"]
                                         .ToObject<Dictionary<string,Champion>>();

and you have to fix "id" property of Champion class, it should be a string

public class Champion
{
    [JsonProperty("id")]
    public string Id { get; set; }
    //..... other properties
}

or you can convert data to List

List<Champion> champions =  ((JObject) JObject.Parse(json)["data"]).Properties()
                                      .Select(p=>p.Value.ToObject<Champion>()).ToList();
Serge
  • 40,935
  • 4
  • 18
  • 45