0

I had a working example parsing a JSON string in a KeyValue class.

For some reason now it is returning 0's and/or nulls.

public class KeyValue { //example JSON string: {"SomeCoolPlayerName":{"id":32179899,"name":"SomeCoolPlayerName","profileIconId":547,"summonerLevel":30,"revisionDate":1396953413000}} public long id {get; set;} public string name {get; set;} public int profileIconId {get; set;} public long summonerLevel {get; set;} public long revisionDate {get; set;} // public List<int> champions {get; set;} } 

Could the JSON string have changed? It is getting populated when I debug I can see it so I added it as a comment above ^^

The web page code is pretty simple also:

 try { WebClient client = new WebClient(); var strJSON = client.DownloadString("http://prod.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" + SummonerName + "?api_key=blanked_on_purpose"); JavaScriptSerializer ser = new JavaScriptSerializer(); var KeyValue = ser.Deserialize<KeyValue>(strJSON); var summonerId = KeyValue.id; //var summonerLevel = KeyValue.summonerLevel; txtSummonerId.Text = summonerId.ToString(); } 

What must I do to get the summonerId correctly again?

2
  • 2
    What does strJSON have when you debug? Commented Apr 8, 2014 at 20:06
  • Are you sure your example JSON string is accurate? According to the documentation, the key in the map returned from that API is all lower case. Commented Apr 8, 2014 at 20:40

2 Answers 2

2

In the JSON you gave, the id is not in the root object, but is nested under the name SomeCoolPlayerName. This can be represented by a dictionary pretty easily:

var strJSON = @"{""SomeCoolPlayerName"":{""id"":32179899,""name"":""SomeCoolPlayerName"",""profileIconId"":547,""summonerLevel"":30,""revisionDate"":1396953413000}}"; JavaScriptSerializer ser = new JavaScriptSerializer(); var dict = ser.Deserialize<Dictionary<string, KeyValue>>(strJSON); // use this if you know the name var summonerId = dict["SomeCoolPlayerName"].id; // 32179899 // or this if you know there's only one value in the dictionary var summonerId = dict.Single().Value.id; // 32179899 
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, the data has changed. From the Change Announcement:

New API Version for Summoners

...

The response will be map of each summoner ID (or name) to the JSON object containing the requested summoner data.

...

You had to have changed the version number in your call to DownloadString(). It has been returning a Map since v1.3, and you are calling v1.4. When API version numbers change, you should expect the response to change, and check the documentation to see if it has.

Now that your item is contained in a Map, you'll need to deserialize it to a Dictionary<string, KeyValue>. According to the documentation, the key "is the summoner name in all lower case and with spaces removed".

string key = SummonerName.ToLower().Replace(" ", ""); var KeyValue = ser.Deserialize<Dictionary<string, KeyValue>>(strJSON)[key]; 

(Personally, I'd rename your KeyValue class to Summoner or SummonerDto to match the API.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.