JSON data returned from WCF service contains escape characters

主宰稳场 提交于 2019-11-28 02:10:53
Brian Rogers

Your JSON is getting double-serialized. (The extra backslashes in the JSON are symptomatic of this.)

Notice in the first version, you do not make a call to serialize the return object inside GetAllStatus(), but in InvokeGet() you do make a call to deserialize it. And that works. So, somehow the return object from GetAllStatus must be getting serialized automatically. It should be clear that WCF must be handling the serialization of your return object for you.

In the second version, you manually serialize the return object inside GetAllStatus() using JsonConvert.SerializeObject(). Since we just established that WCF is also doing serialization, you end up with double-serialized JSON.

To make it work, you need to ensure that the output is only serialized once. Presumably you had a reason to start using Json.Net in your service, so I would look for a way to replace the WCF serializer with JSON.Net. You don't have to look far-- there are quite a few questions and answers on StackOverflow that deal with this very topic. Here are few ideas:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!