Json.Net意外的字符(" \")我的序列化实体(实体框架)时, [英] Json.Net unexpected characters ("\") when serializing my entities (Entity framework)
问题描述
我使用的是优秀的Json.Net库序列由实体框架生成我的实体。我用下面的代码,这样做的:
I am using the excellent Json.Net library to serialize my entities generated by entity framework. I use the following code to do so :
using (MyVoucherEntities context = new MyVoucherEntities())
{
List<MyObject> list = context.MyObjects.ToList();
string json = JsonConvert.SerializeObject(list);
}
一切顺利我的意思是,对象除了网友认为正确的序列化:它增加了逃避,让有噩梦我在客户端反序列化时,字符\。
Everything goes well I mean, the objects are correctly serialized except one think : it adds escape characters "\" that makes me having nightmare when deserializing on the client side.
[
{
\"$id\": \"1\",
\"CreationDate\": \"\\\/Date(1293186324257+0000)\\\/\",
\"ImageUrl\": \"http:\/\/www.google.com\",
\"Title\": \"Here is a title\"
} ]
有谁知道为什么,我如何能摆脱这些转义字符的斜杠\?
Does anybody know why and how I can get rid of these escape characters slash "\" ?
非常感谢!
推荐答案
好吧,我找到了原因,为什么我在我的字符串转义字符(\)......我的序列化对象后,我的JSON字符串,通过返回到客户端应用程序一个WCF。显然,WCF被自动发送到网络之前加入这些字符的字符串。这是一个默认行为,显然是必须的。
Ok I found the reason why I had escape characters in my string ("\") ... After serializing my objects, I am returning the json string to the client app through a WCF. Apparently, WCF is automatically adding these characters to the string before sending it to the network. It is a default behavior and is apparently mandatory.
由于我不希望这些转义字符,解决办法是改变服务的返回类型为Stream等,返回一个内存流里面的JSON字符串。它完美的作品,是安静的快。
As I didn't want these escape characters, the workaround is to change the return type of the service to Stream and so, returning your json string inside a memory stream. It works perfectly and is quiet fast.
THX @乔恩飞碟双向和@克里斯对你有所帮助!
Thx @Jon Skeet and @Chris for your help !
这篇关于Json.Net意外的字符(" \")我的序列化实体(实体框架)时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!