Recommended way to read Entity data? (Unity, C#)
-
Hi, I'm experimenting with brainCloud in Unity (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.
Here's some sample code with questions in the comments:string entityId = "whatever"; SuccessCallback successCallback = (jsonResponse, cbObject) => { // 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<Dictionary<string, object>>(jsonResponse); var entityDict = responseDict["data"] as Dictionary<string, object>; var myDataDict = entityDict["data"] as Dictionary<string, object>; // 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<MyCustomClass>(myDataJson); // Now I have my data in the correct form! }; FailureCallback failureCallback = (status, reasonCode, jsonError, cbObject) => {}; _bc.GlobalEntityService.ReadEntity(entityId, successCallback, failureCallback);Seems like there's probably an easier way to do it, just wondering what you recommend?
Thanks! -
Hi Henry,
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.
-
@Henry-Smith said in Recommended way to read Entity data? (Unity, C#):
Sorry to bring back an old thread, but I'm just learning the platform and I thought I'd share my solution to this.
First, define a generic class to handle the raw json response data:
eg. my Tut1_AddTwoNumbers api returns:{"data":{"response":{"answer":24},"success":true},"status":200}public class Response<T> { public ResponseData<T> data; public int status; } public class ResponseData<T> { public T response; public bool success; }Then for each api call that you have, define a class that contains the fields that are custom to that response.
public class Tut1_AddTwoNumbersResponse { public int answer; }Now, you call your function and handle the response like this:
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) => { var responseObject = JsonReader.Deserialize<Response<Tut1_AddTwoNumbersResponse>>(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) => { Debug.Log(string.Format("Failed | {0} {1} {2}", status, code, error)); }; // Call the script _bc.ScriptService.RunScript(scriptName, jsonScriptData, successCallback, failureCallback); }this line is the important part:
JsonReader.Deserialize<Response<Tut1_AddTwoNumbersResponse>>(response);Hope that helps someone!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login