- Home /
Using SimpleJson included with UnitySocketIO-WebSocketSharp
I'm currently using UnitySocketIO-WebSocketSharp to send Json datas from server to my Unity clients through SocketIO. Socket stuff is working fine and I receive message every half seconds in JSON format but I can't process received datas easily.
I'm new to Json with unity and I would like to use the implementation included with UnitySocketIO-WebSocketSharp but I could only get my Json data as string and I didn't find any function to retrieve values from a key or a way to get Json object as an array.
I can only do this with received datas:
datas.Json.ToJsonString();
Long strings are a pain to deal with using String.Split() and String.Contains()... Did someone ever work with this version of SimpleJson and can write an example to parse my datas and retrieve key/value pairs ?
Answer by Landern · Nov 11, 2014 at 01:52 PM
This library which is NOT the same as @Bunny83 (found here) version. The key look up is done through the indexer which you can find on line:153/235. If you use anything that would make the object default out as a string(.ToString() method call for instance), it will serialize the json object as a string(see line:343).
So, say you have constructed/instantiated a SimpleJson object called sj and you want the user name(Key "UserName").
string user = sj["UserName"]; // Thats it.
There is also a TryGetValue(line:217).
string userName = string.Empty;
sj.TryGetValue("UserName", out userName);
Ok so it seems SimpleJson is a static class I cannot instanciate and the object I get is a JsonEncodedEvent$$anonymous$$essage obtain from I$$anonymous$$essage interface with data.Json, that have the ToJsonString() method.
I see static function DeserializeObject(string json) that return an object but I just want something to find value from key.
I also try something like this but it leads to unresolved error with "data" object
IEnumerable argsList = data.Json.GetArgsAs<string>();
Can you show me how to instanciate a SimpleJson instance as you suggest ?
Answer by NivekJump · Nov 11, 2014 at 02:26 PM
Hi!! This script its very usefull if you want to manipulate JSON in all the posibbles ways. http://wiki.unity3d.com/index.php?title=JSONObject
Thanks for the link but how can I make a JSONObject from my JsonEncodedEvent$$anonymous$$essage data ? Does it can be created from a json string ?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Deserialize nested Json data 1 Answer
CS8025 error 1 Answer
JSONUtility creates a null object of subclass even when no data 2 Answers
Parsing Facebook graph JSON 1 Answer