Good day. I can not find how to organize data exchange on websocket in a unit in a json-format. Tell me please.

I figured out how to connect a web socket, but I can’t find how to process the received message which is in standard json format.

    1 answer 1

    Unity has a special utility ( JsonUtility ) for working with jones.
    You need its FromJson method, which will accept the string sent to you via the socket and return the class you need (well, or try to do it).

    string json = //тут вы формирует его из полученных байтов MyClass class = JsonUtility.FromJson<MyClass>(json); if (class != null) { class.DoCool("Мы сделали это!"); } else { LogService.PushError("Где json, Лебовски?!"); } 
    • Thank. The server part is written in another language. And this class, as I understand it, is engaged in the serialization of unity objects. - Mr Fix
    • I would just parse into ordinary objects like strings and dictionaries. And from this it will be possible to create objects. Or am I misunderstanding something? - Mr Fix
    • @Mrfix, this utility tries to turn json into a specific class. It does not really matter how this json was obtained, the main thing is that you try to turn it into a structure in which the same names and types of serializable fields are M. Green
    • Thought understood. I will understand, thanks. - Mr Fix