<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Deserializing the JSON matchState into a custom object]]></title><description><![CDATA[<p dir="auto">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).</p>
<p dir="auto">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&lt;string, object&gt; but I'm wondering if there's a more straightforward approach.</p>
<p dir="auto">Here's an example of the JSON:</p>
<pre><code>"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": []
			}
		]
	}
}
</code></pre>
<p dir="auto">And here's what my classes look like:</p>
<pre><code>[System.Serializable]
public class JsonMatchState
{
    public int turnNumber { get; set; }
    public int teamTurn { get; set; }
    public JsonGameState gameState { get; set; }
    public List&lt;JsonPreviousTurn&gt; previousTurns { get; set; }
}

[System.Serializable]
public class JsonGameState
{
    public List&lt;JsonTile&gt; tiles { get; set; } = new();
}

[System.Serializable]
public class JsonPreviousTurn
{
    public int teamId { get; set; } = 1;
    public List&lt;JsonTile&gt; 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; }
}
</code></pre>
<p dir="auto">Here are a few of the approaches I've tried:</p>
<pre><code>var data = JsonReader.Deserialize&lt;Dictionary&lt;string, object&gt;&gt;(response)["data"] as Dictionary&lt;string, object&gt;;

//1
JsonMatchState jsonMatchState = data["matchState"] as JsonMatchState;

//2
string matchStateString = JsonConvert.SerializeObject(data["matchState"]);
JsonMatchState jsonMatchState = JsonConvert.DeserializeObject&lt;JsonMatchState&gt;(matchStateString);

//3
string matchStateString = JsonUtility.ToJson(data["matchState"]);
JsonMatchState jsonMatchState = JsonUtility.FromJson&lt;JsonMatchState&gt;(matchStateString);

//4
JsonMatchState jsonMatchState = JsonReader.Deserialize&lt;Dictionary&lt;string, object&gt;&gt;(response)["matchState"] as JsonMatchState;
</code></pre>
]]></description><link>https://forums.getbraincloud.com/topic/186/deserializing-the-json-matchstate-into-a-custom-object</link><generator>RSS for Node</generator><lastBuildDate>Fri, 17 Jul 2026 23:19:50 GMT</lastBuildDate><atom:link href="https://forums.getbraincloud.com/topic/186.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 10 Oct 2023 17:20:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Deserializing the JSON matchState into a custom object on Wed, 11 Oct 2023 15:26:24 GMT]]></title><description><![CDATA[<p dir="auto">Hello Andrey and thank you for your inquiry,</p>
<p dir="auto">For an approach like this I would recommend grabbing the matchState as a Dictionary&lt;string,object&gt; and then making a new instance of JsonMatchState and filling in each field one at a time.</p>
<p dir="auto">An example of this would look like to get one of the fields:</p>
<pre><code>JsonMatchState matchState = new JsonMatchState();
var matchState = data["matchState"] as Dictionary&lt;string, object&gt;;
matchState.turnNumber = (int) matchState["turnNumber"];
</code></pre>
<p dir="auto">Hope this helps and please let us know if you have further questions or inquiries about this.</p>
]]></description><link>https://forums.getbraincloud.com/post/542</link><guid isPermaLink="true">https://forums.getbraincloud.com/post/542</guid><dc:creator><![CDATA[Franco Lago]]></dc:creator><pubDate>Wed, 11 Oct 2023 15:26:24 GMT</pubDate></item></channel></rss>