D
D
durnevdanya2017-05-29 18:28:31
C++ / C#
durnevdanya, 2017-05-29 18:28:31

How to convert JSONObject to string on Unity with socket.io?

Hello. There is a client and server part. I send data from the server and I want an action to take place on the client based on this data.
Server

var player = {
    x: 25,
    y: 10,
    z: 22
  }

  socket.emit('spawn', {xCoords:player.x,yCoords:player.y,zCoords:player.z});

Customer
void Start(){
    socket = GetComponent<SocketIOComponent>();
    socket.On("spawn", onSpawn);
  }
  void onSpawn(SocketIOEvent e){
    string playerX = e.data.GetField("xCoords").toString();
    Instantiate(playerPrefab);
  }

The problem is that I am receiving a JSONObject type from the server, but I need a string. How to be? toString() - doesn't work, various ConvertTos too. Thanks
PS - Then I want to get a line, and then translate it into Float. But this is solvable, but stuck with this

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2017-05-29
@durnevdanya

https://docs.unity3d.com/ScriptReference/JsonUtili...
make first json by format. pass exactly Json
{"xCoords":"player.x","yCoords":"player.y","zCoords":"player.z"} from the server - for example, if you want to transfer strings.
Here you can check that everything is parsed correctly.
well, then it’s easier - work with jison as it says in the documentation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question