itisnajim / SocketIOUnity

A Wrapper for socket.io-client-csharp to work with Unity.
MIT License
393 stars 67 forks source link

OnUnityThread data can't convert to list. #7

Closed DamaSistem closed 2 years ago

DamaSistem commented 2 years ago

Hello, I am trying to convert the object from the server into a list element, but I have not been successful. Here is my code. ` socket.OnUnityThread("friendsStatus", (data) => { //var friends = JsonConvert.DeserializeObject<List>(data.GetValue().GetRawText(), new JsonSerializerSettings //{ // TypeNameHandling = TypeNameHandling.Auto //});

        Debug.Log(data.GetValue()); //  Output of the log : [{"indexnumber":2,"status":0},{"indexnumber":3,"status":0}]  
    });`

How to reach like friends[0].status. Thanks a lot.

DamaSistem commented 2 years ago

Hello, I am trying to convert the object from the server into a list element, but I have not been successful. Here is my code. ` socket.OnUnityThread("friendsStatus", (data) => { //var friends = JsonConvert.DeserializeObject(data.GetValue().GetRawText(), new JsonSerializerSettings //{ // TypeNameHandling = TypeNameHandling.Auto //});

        Debug.Log(data.GetValue()); //  Output of the log : [{"indexnumber":2,"status":0},{"indexnumber":3,"status":0}]  
    });`

How to reach like friends[0].status. Thanks a lot.

I figure out :D My bad. This is how i should have done when deserializing data.GetValue().ToString().

itisnajim commented 2 years ago

Hi,

you can get the first object(with the index 0) within the list like this:

data.GetValue<FriendStatus>(0);

see: https://github.com/doghappy/socket.io-client-csharp#quick-start

have a good day;