I am having some trouble figuring out a clean (as possible) way to deserialize some JSON data in a particular format. I want to deserialize the data to strongly typed data object classes, pretty flexible regarding the specifics of this. Here is an example of what the data looks like:
{
"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 parts I am unsure about are:
- Would the collection of players be considered a dictionary? The username could serve as the key, but the value is throwing me off since it would be a mixed collection of string and integer values.
- A player is comprised entirely of unnamed values. I have pretty much always worked with JSON data that had named properties and values (ex. timestamp, total_players, etc. at the very top)
Say I have a top level class like this:
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 the Player object look like given that it is basically a key/value with the username serving as the key, and the value being a collection of mixed integers and strings? The data for each player element is always in the same order, so I know that the first value in the collection is their UniqueID, the second value is a player description, etc. I would like the player class to be something like this:
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 am sure this is a pretty straightforward thing to do using JSON.NET, which is why I wanted to avoid any of the ideas I had on how to accomplish this. What I came up with would have been un-elegant and probably error prone to some degree during the serialization process.
EDIT
Here are the classes that get 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; }
}
What is not clear to me is how do I deserialize the JSON data in the "players" element as a List with Player1Username being a simple string property on the Player object. As for the collection of intermixed strings and integers, I am confident I can get those into individual properties on the Player object without issue.
Question&Answers:
os