Reputation: 49
I am attempting to deserialize a JSON response from a webserver which I have no control over. As you can see below the resulting object is returned as a JSONObject. The issue with this is that the key of each object is unique to every player. As far as I can tell I am required to create a separate class for each unique JSONObject to deserialize the string properly which simply would not work considering the key for each object is unique to each created account that is returned from the webserver. I want to know if it is possible to deserialize the JSONObject "players" as a JSONArray or if I am overthinking this a bit too much. The ammount of players returned in the list also varies for up to a maximum of 32.
{
"players": {
"930153636": {
"name": "ur_moms_cookies",
"tag": "",
"rank": 62,
"score": 1332,
"kills": 9,
"deaths": 2,
"squad": 1,
"role": 1
},
"250721735": {
"name": "PyRobotic",
"tag": "",
"rank": 5,
"score": 1786,
"kills": 8,
"deaths": 4,
"squad": 2,
"role": 1
},
"1035406266": {
"name": "ShrapnalFire",
"tag": "GaGu",
"rank": 60,
"score": 1455,
"kills": 8,
"deaths": 7,
"squad": 1,
"role": 1
},
"239767512": {
"name": "NeoEllis",
"tag": "",
"rank": 77,
"score": 3244,
"kills": 20,
"deaths": 6,
"squad": 3,
"role": 1
},
"936400786": {
"name": "antiares51",
"tag": "",
"rank": 140,
"score": 4720,
"kills": 17,
"deaths": 2,
"squad": 2,
"role": 1
},
"355364783": {
"name": "BombKat11",
"tag": "",
"rank": 16,
"score": 1347,
"kills": 9,
"deaths": 5,
"squad": 4,
"role": 1
},
"803046096": {
"name": "thejoedude",
"tag": "FSJ",
"rank": 66,
"score": 1294,
"kills": 8,
"deaths": 8,
"squad": 4,
"role": 1
},
"788221305": {
"name": "Tiyou33",
"tag": "",
"rank": 45,
"score": 1927,
"kills": 4,
"deaths": 5,
"squad": 2,
"role": 1
},
"353394766": {
"name": "DKOfTFC",
"tag": "RSPN",
"rank": 119,
"score": 1895,
"kills": 9,
"deaths": 4,
"squad": 4,
"role": 1
},
"370847289": {
"name": "shohet10",
"tag": "",
"rank": 40,
"score": 1570,
"kills": 7,
"deaths": 7,
"squad": 6,
"role": 1
},
"206872615": {
"name": "me94132",
"tag": "",
"rank": 60,
"score": 2045,
"kills": 7,
"deaths": 4,
"squad": 3,
"role": 1
},
"332317870": {
"name": "frehgv",
"tag": "",
"rank": 48,
"score": 713,
"kills": 3,
"deaths": 4,
"squad": 4,
"role": 1
},
"1144758913": {
"name": "MudCruncher_78",
"tag": "",
"rank": 61,
"score": 1822,
"kills": 7,
"deaths": 5,
"squad": 6,
"role": 1
},
"603412175": {
"name": "UsedTissue",
"tag": "",
"rank": 119,
"score": 1551,
"kills": 10,
"deaths": 1,
"squad": 1,
"role": 1
},
"1655466910": {
"name": "JackTheRipp3r2",
"tag": "",
"rank": 27,
"score": 797,
"kills": 2,
"deaths": 3,
"squad": 8,
"role": 1
},
"869745436": {
"name": "Negibou",
"tag": "",
"rank": 139,
"score": 4792,
"kills": 14,
"deaths": 4,
"squad": 2,
"role": 1
},
"447652633": {
"name": "RufusTheRabbit",
"tag": "",
"rank": 91,
"score": 1468,
"kills": 7,
"deaths": 4,
"squad": 4,
"role": 1
},
"352650707": {
"name": "DJ1101",
"tag": "",
"rank": 28,
"score": 847,
"kills": 5,
"deaths": 6,
"squad": 3,
"role": 1
},
"855246326": {
"name": "-Frigadier-",
"tag": "",
"rank": 109,
"score": 667,
"kills": 1,
"deaths": 6,
"squad": 1,
"role": 1
},
"255855183": {
"name": "Asakajim",
"tag": "",
"rank": 6,
"score": 514,
"kills": 0,
"deaths": 8,
"squad": 8,
"role": 1
},
"248119620": {
"name": "Kokovec",
"tag": "FCM",
"rank": 121,
"score": 891,
"kills": 5,
"deaths": 3,
"squad": 8,
"role": 1
},
"348016421": {
"name": "pandaymd",
"tag": "",
"rank": 122,
"score": 1595,
"kills": 6,
"deaths": 9,
"squad": 2,
"role": 1
},
"250545778": {
"name": "Vulkkann",
"tag": "",
"rank": 40,
"score": 2323,
"kills": 14,
"deaths": 8,
"squad": 3,
"role": 1
},
"949941984": {
"name": "WarchiefBigzb",
"tag": "",
"rank": 70,
"score": 1013,
"kills": 5,
"deaths": 3,
"squad": 9,
"role": 1
},
"268773188": {
"name": "Brutuka",
"tag": "",
"rank": 48,
"score": 952,
"kills": 5,
"deaths": 4,
"squad": 5,
"role": 1
},
"801259600": {
"name": "gpc4567",
"tag": "",
"rank": 109,
"score": 1710,
"kills": 11,
"deaths": 5,
"squad": 3,
"role": 1
},
"338093557": {
"name": "Zelios86",
"tag": "",
"rank": 140,
"score": 1400,
"kills": 7,
"deaths": 4,
"squad": 8,
"role": 1
},
"364705773": {
"name": "Fleischwolfx",
"tag": "",
"rank": 121,
"score": 1863,
"kills": 13,
"deaths": 2,
"squad": 5,
"role": 1
},
"173524306": {
"name": "BlackDynamite901",
"tag": "",
"rank": 75,
"score": 583,
"kills": 4,
"deaths": 2,
"squad": 9,
"role": 1
},
"339527854": {
"name": "Xixus",
"tag": "",
"rank": 21,
"score": 0,
"kills": 0,
"deaths": 0,
"squad": 1,
"role": 1
},
"1667184018": {
"name": "BckBone71",
"tag": "",
"rank": 35,
"score": 98,
"kills": 1,
"deaths": 1,
"squad": 5,
"role": 1
}
}
}
Thus far I have tried a few variations of something along the lines of the below code snippet which of course does not work since the players object isn't an array.
public class Team
{
public int faction;
[JsonProperty("players")]
public List<Player> players { get; set; }
}
public class Player
{
public string name;
public string tag;
public int rank;
}
Upvotes: 2
Views: 1625
Reputation: 5119
Try something like this:
var players = new List<Player>();
dynamic dObject = JObject.Parse(JSON);
foreach (var property in dObject.players) {
var player = property.Value;
var playerModel = new Player {
Name = player.name,
Tag = player.tag,
Rank = player.rank
};
players.Add(playerModel);
}
Where JSON
is your Json object. In this case, I just made it a string literal to test your object. This is using NewtonSoft.Json for the parsing.
Once you get the player, you should have access to all of its properties and not have to worry about the unique player ID.
Here is the .NET fiddle: https://dotnetfiddle.net/9iOT5w.
Upvotes: 1