I get a json object that looks like the following.
{
"result": {
"status": 1,
"teams": [
{
"team_id": 1838315,
"name": "Team Secret",
"tag": "Secret",
"time_created": 1408993713,
"rating": "inactive",
"logo": 543025270456493060,
"logo_sponsor": 540768028333677400,
"country_code": "",
"url": "http://www.teamsecret.gg/",
"games_played_with_current_roster": 0,
"player_0_account_id": 41231571,
"player_1_account_id": 73562326,
"player_2_account_id": 82262664,
"player_3_account_id": 86745912,
"player_4_account_id": 87278757,
"admin_account_id": 5390881,
"league_id_0": 1803,
"league_id_1": 1886,
"league_id_2": 1936,
"league_id_3": 1942,
"league_id_4": 2129,
"league_id_5": 2140,
"league_id_6": 2158,
"league_id_7": 2339,
"league_id_8": 2418,
"league_id_9": 2661,
"league_id_10": 2877
}
]
}
}
I want to use the Newtonsoft json.net libaray to deserialize the object. Most of it is easy but i am not really sure how to handle the player_X_account_id and the league_id_X.
Both have a more or less unlimited amount of elements. But as the are not mapped as a list but single elements I am not really sure how to map them into my .Net Object.
I could create a hundred members of each sort but that wouldn't be especially nice. Preferable i would map them to some sort of Collection, but i have no idea how to accomplish or if it is even possible.
Any idea how to do that?
EDIT: I have no control over the json object i get, it's a call to the SteamWebAPI
You can use extension data feature of JSON.NET :
public class Team
{
public int Team_Id { get; set; }
public string Name { get; set; }
public string Tag { get; set; }
public string Url { get; set; }
//other properties
[JsonExtensionData]
public IDictionary<string, JToken> AdditionalData { get; set; }
}
Then you can check the properties for league and player ids by enumerating AdditionalData property.
In a perfect world you would have control over the JSON
structure that you're receiving and you could make it look like this
..."league_id" [['1803','1886','1936']]...
instead of that league_id_x thing that you got going on but I don't know if you got control over that.
In the other hand you could use dynamic variables:
dynamic jsonDe = JsonConvert.DeserializeObject(json);
But it has many downfalls as well.
I think you can convert it to an dictionary object and access the data.
Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
foreach (KeyValuePair<string, object> data in values)
{
var key=data.key;
var value=data.value;
if(key =="teams")
{
foreach(KeyValuePair<string, object> team in data )
{
//do your stuffs here................
}
}
}
But it will be good for you if you keep the player_X_account_id
and the league_id_X
in teams as object like :
"teams": [
{
your data......,
"players":{
"player_0_account_id": 41231571,
"player_1_account_id": 73562326,
"player_2_account_id": 82262664,
"player_3_account_id": 86745912,
"player_4_account_id": 87278757,
}
"admin_account_id": 5390881,
"league":{
"league_id_0": 1803,
"league_id_1": 1886,
"league_id_2": 1936,
"league_id_3": 1942,
"league_id_4": 2129,
"league_id_5": 2140,
"league_id_6": 2158,
"league_id_7": 2339,
"league_id_8": 2418,
"league_id_9": 2661,
"league_id_10": 2877
}
}
]
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.