0

I have a web socket on my server and that sockets return list of player infos to the all clients. After I get the data from websocket ı cant deserilialize my data cause c# adding some '"' characters to my data. And ı cant remove that characters

"\"[{\\\"Id\\\":\\\"\\\",\\\"Location\\\":{\\\"X\\\":60.0,\\\"Y\\\":60.0,\\\"Z\\\":10.0},\\\"Rotation\\\":{\\\"X\\\":10.0,\\\"Y\\\":10.0,\\\"Z\\\":10.0},\\\"State\\\":\\\"walk\\\",\\\"IsAttacking\\\":false,\\\"PlayerName\\\":\\\"Sadooo\\\",\\\"Health\\\":100,\\\"LastSync\\\":637598981481744771}]\""

My Json string in debug mode look like this. How can i clear this string and deserialize. I have this class in my project property names and everything okey ı just need the get rid of \ character and " character at the beginning and end of data.

  public class PlayerInfo
    {
        //public string IpAdress { get; set; }
        public string Id { get; set; }
        public MyVector Location { get; set; }
        public MyVector Rotation { get; set; }
        public string State { get; set; }
        public bool IsAttacking { get; set; }
        public string PlayerName { get; set; }
        public int Health { get; set; }
        public long LastSync { get; set; }

    }
    public class MyVector
    {
        public float X { get; set; }
        public float Y { get; set; }
        public float Z { get; set; }
    }

And this is my class

JsonConvert.SerializeObject(myList);//This is how i serilaized  to my data. After that I convert this string to byte array and send to the socket.

On Client side I get the byte array from socket and convert string like this

var data=Encoding.UTF8.GetString(result.Buffer);

than deserilialize like this

 var players= JsonConvert.DeserializeObject<List<PlayerInfo>>(data,new JsonSerializerSettings { NullValueHandling=NullValueHandling.Ignore});

This is data in text visualizer
This is data in text visualizer
Thanks for your help.

Sadullah DOĞAN
  • 335
  • 2
  • 10
  • 1
    You do not show any code that serializes the data or any additional manipulation before you send the data over the socket. You also do not show any of your code to read or deserialize the data into these classes. We cannot guess what you have done. – crashmstr Jun 21 '21 at 19:04
  • There are too many escaped backslashes there. It looks like you copied your json string from the watch/locals window. What do you get when you look at the string in the _text visualizer_? For example: When I do `var str = @"Hello\World";`, my [watch window shows the escape characters but the text visualizer doesn't.](https://i.stack.imgur.com/QthxH.png) – Pranav Hosangadi Jun 21 '21 at 19:17
  • Its look like this https://i.stack.imgur.com/W5O6m.png ı need the remove " characters from this ı suppose but ı couldnt do it . – Sadullah DOĞAN Jun 21 '21 at 19:22

2 Answers2

1

Just try this:

var players= JsonConvert.DeserializeObject<List<PlayerInfo>>(
JsonConvert.DeserializeObject<string>(data),
new JsonSerializerSettings { NullValueHandling=NullValueHandling.Ignore});
sa-es-ir
  • 3,722
  • 2
  • 13
  • 31
0

It clearly looks like multiple level of serialisation, it looks like you are serializing object to json string before passing it to websocket library where it is serializing as string.

In this case try multiple level of deserialization or remove unnecessary serialisation.

mbshambharkar
  • 386
  • 4
  • 13
  • Before sending the object to the socket ı seriliaze object to string. And socket accept byte array. After seriliazing objet to string i am converting string to byte array than send to the socket. After receiving data from client ı convert byte to string after that trying to deserialize. Could this be causing the error ? – Sadullah DOĞAN Jun 21 '21 at 19:06
  • Well it looks like before creating byte array, data is being serialized two times , that's why you are getting " as string identifier and \ as escape characters in Json. – mbshambharkar Jun 21 '21 at 19:15
  • Could you please share code of serialisation and deserialization ? – mbshambharkar Jun 21 '21 at 19:18
  • I added to my question . – Sadullah DOĞAN Jun 21 '21 at 19:23