Escape characters after Serializing using Json

后端 未结 3 665
梦谈多话
梦谈多话 2021-01-27 11:15

Y is my object being serialized in a weird manner while using newtonsoft.json from ASP.Net Web API?

var s = JsonConvert.SerializeObject(request, For         


        
相关标签:
3条回答
  • 2021-01-27 11:21

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

    Either:

    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 a bug in Newtonsoft where JObject.FromObject ignores the DateFormatString in JsonSerializer.

    0 讨论(0)
  • 2021-01-27 11:44

    There's nothing to be worried about here. This is the correct serialized object json. It is appearing like this because you are viewing this in visual studio by hovering variable to view its value, because in c# \" is used to represent a " in a string. When you will write this value in a text file(just to test actual value) , you will see what is it's actual value as:

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

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

    0 讨论(0)
  • 2021-01-27 11:46

    It's because you have serialized it twice, can you post more of your code or skip calling SerializeObject completely

    0 讨论(0)
提交回复
热议问题