<?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[Recommended way to read Entity data? (Unity, C#)]]></title><description><![CDATA[<p dir="auto">Hi, I'm experimenting with brainCloud in <strong>Unity</strong> (C#) and I'm trying to read a Global Entity. I can get the JSON of the Entity object but then what's the best way to access the "data" key inside? I can do it in a convoluted way but I'm hoping there's a friendlier way using the API.<br />
Here's some sample code with questions in the comments:</p>
<pre><code>string entityId = "whatever";
SuccessCallback successCallback = (jsonResponse, cbObject) =&gt;
{
	// Unity's JsonUtility doesn't support Dictionaries so I guess I should use brainCloud's internal JsonReader?
	// And is there a more specific class I can Deserialize to, or should I just use generic Dictionaries?
	var responseDict = JsonReader.Deserialize&lt;Dictionary&lt;string, object&gt;&gt;(jsonResponse);
	var entityDict = responseDict["data"] as Dictionary&lt;string, object&gt;;
	var myDataDict = entityDict["data"] as Dictionary&lt;string, object&gt;;

	// Now I have my data as a Dictionary, but ideally I want to deserialize it into my own custom class type...
	// So should I *re-serialize* it back to JSON so I can read it? This seems unnecessary.
	var myDataJson = JsonWriter.Serialize(myDataDict);
	var myData = JsonReader.Deserialize&lt;MyCustomClass&gt;(myDataJson);

	// Now I have my data in the correct form!
};
FailureCallback failureCallback = (status, reasonCode, jsonError, cbObject) =&gt; {};

_bc.GlobalEntityService.ReadEntity(entityId, successCallback, failureCallback);
</code></pre>
<p dir="auto">Seems like there's probably an easier way to do it, just wondering what you recommend?<br />
Thanks!</p>
]]></description><link>https://forums.getbraincloud.com/topic/88/recommended-way-to-read-entity-data-unity-c</link><generator>RSS for Node</generator><lastBuildDate>Wed, 19 Aug 2026 18:26:35 GMT</lastBuildDate><atom:link href="https://forums.getbraincloud.com/topic/88.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 18 May 2020 18:37:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Recommended way to read Entity data? (Unity, C#) on Sat, 24 Oct 2020 19:46:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/henry-smith" aria-label="Profile: Henry-Smith">@<bdi>Henry-Smith</bdi></a> said in <a href="/post/288">Recommended way to read Entity data? (Unity, C#)</a>:</p>
<p dir="auto">Sorry to bring back an old thread, but I'm just learning the platform and I thought I'd share my solution to this.</p>
<p dir="auto">First, define a generic class to handle the raw json response data:<br />
eg. my Tut1_AddTwoNumbers api returns:</p>
<pre><code>{"data":{"response":{"answer":24},"success":true},"status":200}
</code></pre>
<pre><code>    public class Response&lt;T&gt;
    {
        public ResponseData&lt;T&gt; data;
        public int status;
    }
    public class ResponseData&lt;T&gt;
    {
        public T response;
        public bool success;
    }
</code></pre>
<p dir="auto">Then for each api call that you have, define a class that contains the fields that are custom to that response.</p>
<pre><code>    public class Tut1_AddTwoNumbersResponse
    {
        public int answer;
    }
</code></pre>
<p dir="auto">Now, you call your function and handle the response like this:</p>
<pre><code>    public void Tut1_AddTwoNumbers()
    {
        string scriptName = "Tut1_AddTwoNumbers";
        // Anonymous object to supply the params.
        var data = new
        {
            num1 = 16,
            num2 = 8
        };

        var jsonScriptData = JsonWriter.Serialize(data);
        SuccessCallback successCallback = (response, userdata) =&gt;
        {
            var responseObject = JsonReader.Deserialize&lt;Response&lt;Tut1_AddTwoNumbersResponse&gt;&gt;(response);
            //var responseObject = JObject.Parse(response);
            Debug.Log($"Success The answer is '{responseObject.data.response.answer}' | raw_json={response}");
        };
        FailureCallback failureCallback = (status, code, error, userdata) =&gt;
        {
            Debug.Log(string.Format("Failed | {0}  {1}  {2}", status, code, error));
        };
        // Call the script 
        _bc.ScriptService.RunScript(scriptName, jsonScriptData, successCallback, failureCallback);
    }
</code></pre>
<p dir="auto">this line is the important part:</p>
<pre><code>JsonReader.Deserialize&lt;Response&lt;Tut1_AddTwoNumbersResponse&gt;&gt;(response);
</code></pre>
<p dir="auto">Hope that helps someone!</p>
]]></description><link>https://forums.getbraincloud.com/post/350</link><guid isPermaLink="true">https://forums.getbraincloud.com/post/350</guid><dc:creator><![CDATA[Travis Brown-John]]></dc:creator><pubDate>Sat, 24 Oct 2020 19:46:54 GMT</pubDate></item><item><title><![CDATA[Reply to Recommended way to read Entity data? (Unity, C#) on Wed, 20 May 2020 12:11:48 GMT]]></title><description><![CDATA[<p dir="auto">Hi Henry,</p>
<p dir="auto">Yes the data received is in JSON. And brainCloud expects a JSON when you update the entity. What you can do is create a serialize and a deserialize method inside your custom type.</p>
]]></description><link>https://forums.getbraincloud.com/post/289</link><guid isPermaLink="true">https://forums.getbraincloud.com/post/289</guid><dc:creator><![CDATA[David St-Louis 0]]></dc:creator><pubDate>Wed, 20 May 2020 12:11:48 GMT</pubDate></item></channel></rss>