且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除c#中括号和结束括号json之前的倒置逗号

更新时间:2023-02-18 16:20:00

在open括号({)之前有一个引号()这一事实通常意味着该值本身就是一个json字符串,这正是你正在做的事情。当你反序列化时字符串你应该允许ResStrDisplay成为一个字符串,然后将其反序列化到对象中。



问题是json序列化将包含所需的转义字符所以这样做两次将添加进一步的转义字符。如果你试图通过操纵字符串反序列化,你将不得不考虑到这一点。



另一个选项(和我使用的那个)是不预先序列化ResStrDisplay。如果你把它作为一个对象,那么整个WebEmployees对象的序列化将产生你期望的格式。
The fact that there is a quotation mark (") before the open bracket ("{") usually mean that the value is itself a json string, and this is infact exactly what you are doing. When you deserialize the string you should allow ResStrDisplay to be a string and then deserialize that into the object.

The issue is that a json serialization will include escape chars where needed so doing it twice will add further escape chars. if you try to deserialize by manipulating the string you will have to take that into account.

Another option (and the one I use) is to not pre-serialize ResStrDisplay. If you leave it as an object then the serialization of the whole WebEmployees object will produce the format you expect.