0

Im trying to make a program using the League of legends API. The API doesnt return a array of objects. It has different objects for each champions. My question is, how can i get these objects into a c# list without having to make a object for each champion in C#? https://global.api.pvp.net/api/lol/static-data/euw/v1.2/champion?api_key=362039df-6ce4-4e2f-ac3c-27d246d03f45 (This is the json list im getting) I have the following code to convert the objects, but obviously it cannot fill the list.

var webclient = new WebClient();
var strJson = webclient.DownloadString("https://global.api.pvp.net/api/lol/static-data/euw/v1.2/champion?api_key=362039df-6ce4-4e2f-ac3c-27d246d03f45");
var List<ChampionJson> = JsonConvert.DeserializeObjectList<<ChampionJson>>(strJson);

This is the champion class:

public class ChampionJson
{
    public Champion champion { 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 know its probably completely wrong what im doing but im pretty new to Json. I have been trying to get it to work the past two days but i cannot find anything on the internet. Pls help, Thanks in advance!

2
  • Please include a sample of the JSON you're trying to deserialize in your question, and please elaborate on why your code isn't working - "it cannot fill the list" isn't very descriptive. Commented Mar 9, 2016 at 13:11
  • Obviously you need to create same number of objects as the values in json response. Commented Mar 9, 2016 at 13:12

1 Answer 1

1

Your object basically needs to reflect the model the JSON is providing. You are quite close; I would change the ChampionJson object definition to:

public class ChampionJson { public Dictionary<string, Champion> data { get; set; } }

and then use

JsonConvert.DeserializeObject<ChampionJson>(strJson) 

instead of

JsonConvert.DeserializeObjectList<<ChampionJson>>(strJson)
Sign up to request clarification or add additional context in comments.

8 Comments

Whenever I need to write something off hand and want to verify I use dotnetfiddle.net. You can even include packages via nuget. Just a tip.
@Default I had no idea that even existed. I am truly grateful! Thank you! - tested my answer and it works :)
I did what u explained and it gives the var champion no data, it just has null values
Did you rename the Dictionary<string,Champion> property in ChampionJson to 'data', as I wrote? If yes, can you edit your question and paste the code that you've changed?
Here's a dotnetfiddle, with your code working (thanks to @Default) dotnetfiddle.net/nS9qXh
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.