I am having trouble figuring out a clean (as much as possible) way to deserialize JSON data in a specific format. I want to deserialize data into strongly typed classes of data objects, quite flexible regarding the features of this. Here is an example of how the data looks:
{ "timestamp": 1473730993, "total_players": 945, "max_score": 8961474, "players": { "Player1Username": [ 121, "somestring", 679900, 5, 4497, "anotherString", "thirdString", "fourthString", 123, 22, "YetAnotherString"], "Player2Username": [ 886, "stillAstring", 1677, 1, 9876, "alwaysAstring", "thirdString", "fourthString", 876, 77, "string"] } }
The specific details that I'm not sure about are the following:
- Will player gathering be seen as a dictionary? The username can serve as a key, but the value throws me away as it will be a mixed collection of string values ββand integers.
- The player consists solely of unnamed values. I almost always worked with JSON data that called properties and values ββ(e.g. timestamp, total_players, etc. At the very top).
Let's say I have a top level class:
public class ScoreboardResults { public int timestamp { get; set; } public int total_players { get; set; } public int max_score { get; set; } public List<Player> players { get; set; } }
What would a Player object look like, given that it is basically a key / value with a username serving as a key, and the value is a collection of mixed integers and strings? The data for each element of the player is always in the same order, so I know that the first value in the collection is their UniqueID, the second value is the description of the player, etc. I would like the player class to be something like this: / p>
public class Player { public string Username { get; set; } public int UniqueID { get; set; } public string PlayerDescription { get; set; } .... .... .... Following this pattern for all of the values in each player element .... .... }
I'm sure this is a pretty simple thing using JSON.NET, so I wanted to avoid any ideas that I had on how to do this. What I came up with would not be elegant and probably a mistake, prone to some degree during the serialization process.
EDIT
Here are the classes that are generated when using the past as JSON classes, as suggested by snow_FFFFFF :
public class Rootobject { public int timestamp { get; set; } public int total_players { get; set; } public int max_score { get; set; } public Players players { get; set; } } public class Players { public object[] Player1Username { get; set; } public object[] Player2Username { get; set; } }
I donβt understand how I will deserialize the JSON data in the players element as a List with the name Player1Username, which is a simple string property of the Player object. As for the set of mixed strings and integers, I am sure that I can get them in the individual properties of the Player object without any problems.