Reputation: 7805
I am using Riot's API here: https://developer.riotgames.com/api/methods#!/909/3144
That request allows you to give a comma-delimited list of usernames and returns user information.
I am executing my code as follows:
string getUrl = "https://" + this.regionID + ".api.pvp.net/api/lol/" + this.regionID +
"/v1.4/summoner/by-name/" + summoner.Text + "?api_key=" + this.apiKey;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
dynamic json = JsonConvert.DeserializeObject<dynamic>(reader.ReadToEnd());
}
This returns the results as follows:
{
"prorate": {
"id": 20335410,
"name": "ProRate",
"profileIconId": 693,
"revisionDate": 1420864656000,
"summonerLevel": 30
},
"jaxelrod": {
"id": 31034983,
"name": "Jaxelrod",
"profileIconId": 744,
"revisionDate": 1420999923000,
"summonerLevel": 30
}
}
Now, lets say I want to get the ID of the first user returned in the list. I know I can do so using the following code:
json.prorate.id.ToString();
However, I wont necessarily know the index of the first element in the list. In this specific case, its prorate
, but it could be something different on each call. Is there a call I could make to simply retrieve the first element of the array? Something like json.First().id.ToString()
?
Upvotes: 3
Views: 8808
Reputation: 15364
You don't need to use dynamic
var userList = JsonConvert.DeserializeObject < Dictionary<string, User>>(json);
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; }
}
You can also use Linq
var id = JObject.Parse(json).Children().First().Values<int>("id").First();
Upvotes: 3