Escaping characters after serialization using Json

Y - is my object serialized in a weird way when using newtonsoft.json from ASP.Net Web API?

 var s = JsonConvert.SerializeObject(request, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); 

Print a line as shown below

 "{\"head\":{\"version\":\"1.0\",\"serial\":\"20140102,6,125\",\"skinId\":\"Test\"" 

I want to send a Json format string to a third-party service (they only accept Json format ).

Any help is greatly appreciated.

Cheers S

0
source share
3 answers

There is nothing to worry about. This is the correct serialized json object. It looks like this because you are viewing it in visual studio by hanging a variable to view its value, because in C # \ "is used to represent" in a string. When you write this value in a text file (just to check the actual value), you will see that this actual value is like:

  string json="{\"head\":{\"version\":\"1.0\",\"serial\":\"20140102,6,125\",\"skinId\":\"Test\""; File.WriteAllText("c:\\tests on.txt",json) ; 

You will see json in the file what you really want.

+1
source

This is because you serialized it twice, can you post more of your code or skip SerializeObject completely

0
source

I doubt that you are still looking for an answer, but my workaround was to create a JObject with Newtonsoft and pass it on.

Or:

 JObject jBytes = Object.Parse(JsonConvert.SerializeObject(myObject, MyDateTimeFmtString); 

or

 JObject jBytes = JObject.FromObject(myObject, MyJsonSerializer); 

The first case was my second choice, but I think there is an error in Newtonsoft where JObject.FromObject ignores DateFormatString in JsonSerializer.

0
source

Source: https://habr.com/ru/post/972657/


All Articles