Deserializing the JSON matchState into a custom object
-
I'm working in Unity, and from the JSON response I get from calling AsyncMatchService.ReadMatch I want to convert the "matchState" into a custom object of type JsonMatchState. I've tried a few approaches but either get a NullReferenceException or the data populated in the JsonMatchState object is incorrect (default data).
The alternative approach I've heard is to create a new JsonMatchState and read in the values for that class one by one as a Dictionary<string, object> but I'm wondering if there's a more straightforward approach.
Here's an example of the JSON:
"data": { "matchState": { "turnNumber": 1, "teamTurn": 2, "gameState": { "tiles": [ { "gridCoordinates": "0,3", "teamId": 0, "defenseValue": 0, "totalDefenseAquired": 0, "territoryId": 0, "snakeUnitId": 0, "snakeCarcass": false, "nest": false }, { "gridCoordinates": "-2,1", "teamId": 0, "defenseValue": 0, "totalDefenseAquired": 0, "territoryId": 0, "snakeUnitId": 0, "snakeCarcass": false, "nest": false } ] }, "previousTurns": [ { "teamId": 1, "tiles": [ { "gridCoordinates": "0,1", "teamId": 1, "defenseValue": 1, "totalDefenseAquired": 1, "territoryId": 0, "snakeUnitId": 0, "snakeCarcass": false, "nest": false } ] }, { "teamId": 2, "tiles": [] } ] } }
And here's what my classes look like:
[System.Serializable] public class JsonMatchState { public int turnNumber { get; set; } public int teamTurn { get; set; } public JsonGameState gameState { get; set; } public List<JsonPreviousTurn> previousTurns { get; set; } } [System.Serializable] public class JsonGameState { public List<JsonTile> tiles { get; set; } = new(); } [System.Serializable] public class JsonPreviousTurn { public int teamId { get; set; } = 1; public List<JsonTile> tiles { get; set; } = new(); } [System.Serializable] public class JsonTile { public string gridCoordinates { get; set; } public int teamId { get; set; } public int defenseValue { get; set; } public int totalDefenseAquired { get; set; } public int territoryId { get; set; } public int snakeUnitId { get; set; } public bool snakeCarcass { get; set; } public bool nest { get; set; } }
Here are a few of the approaches I've tried:
var data = JsonReader.Deserialize<Dictionary<string, object>>(response)["data"] as Dictionary<string, object>; //1 JsonMatchState jsonMatchState = data["matchState"] as JsonMatchState; //2 string matchStateString = JsonConvert.SerializeObject(data["matchState"]); JsonMatchState jsonMatchState = JsonConvert.DeserializeObject<JsonMatchState>(matchStateString); //3 string matchStateString = JsonUtility.ToJson(data["matchState"]); JsonMatchState jsonMatchState = JsonUtility.FromJson<JsonMatchState>(matchStateString); //4 JsonMatchState jsonMatchState = JsonReader.Deserialize<Dictionary<string, object>>(response)["matchState"] as JsonMatchState;
-
Hello Andrey and thank you for your inquiry,
For an approach like this I would recommend grabbing the matchState as a Dictionary<string,object> and then making a new instance of JsonMatchState and filling in each field one at a time.
An example of this would look like to get one of the fields:
JsonMatchState matchState = new JsonMatchState(); var matchState = data["matchState"] as Dictionary<string, object>; matchState.turnNumber = (int) matchState["turnNumber"];
Hope this helps and please let us know if you have further questions or inquiries about this.