将 Linq 结果直接序列化为 JSON
我正在开发一个 WebService,它执行 linq to sql db 并将结果放入 VAR 变量中。然后我想使用 javascript 序列化程序(c#)将 VAR 内的结果序列化为 json 格式。像这样的事情:
var sb= from p in ent.people .........
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(sb.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, sb);
string json = System.Text.Encoding.Default.GetString(ms.ToArray());
但我得到这样的错误响应:
Type 'System.Data.Objects.ObjectQuery`1[<>f__AnonymousType2d`5[System.String,System.Nu llable`1[System.Int32],System.Nullable`1[System.Int32],System.Int32,System.String]]' cannot be serialized.
考虑使用 DataContractAttribute 属性标记它,并使用 DataMemberAttribute 属性标记您想要序列化的所有成员。如果类型是集合,请考虑使用 CollectionDataContractAttribute 对其进行标记。有关其他支持的类型,请参阅 Microsoft .NET Framework 文档。
如何将 LINQ 结果直接序列化为 JSON? 非常感谢所有的答案! 恩里科
I'm developing a WebService that excecute linq to sql db and put the results into a VAR variable. Then I wanna serialize the result inside VAR to json format using javascript serializer (c#). Something like this:
var sb= from p in ent.people .........
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(sb.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, sb);
string json = System.Text.Encoding.Default.GetString(ms.ToArray());
BUT I GET AN ERROR RESPONSE LIKE THIS:
Type 'System.Data.Objects.ObjectQuery`1[<>f__AnonymousType2d`5[System.String,System.Nu llable`1[System.Int32],System.Nullable`1[System.Int32],System.Int32,System.String]]' cannot be serialized.
Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
HOW CAN I SERIALIZE LINQ RESULTS DIRECTLY TO JSON??
Thanks a lot for all answers!
Enrico
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DataContractJsonSerializer 不支持匿名对象。如果您想序列化匿名对象,可以使用 JavaScriptSerializer 类:
DataContractJsonSerializer doesn't support anonymous objects. If you want to serialize anonymous objects you could use the JavaScriptSerializer class:
您可以使用 Newtonsoft.JSON ,这
是语法
you can use Newtonsoft.JSON for that
heres's the syntax
在我的上一份工作中,我们看到了这种行为,并且需要采取一些措施来解决它。
首先,您需要 IQ 工具包,可以从 CodePlex 免费获取。它的库中有一个“PartialEvaluator”,它可以通过查找始终计算为更简单节点的节点以及用常量替换对“外部闭包”的引用来降低许多表达式树的复杂性。在尝试序列化 IQueryable 之前,您需要先运行它。
然后,为了使用 JSON DataContract 序列化器,您必须将要序列化为 DataContract 的类设置。到处都有关于执行此操作的教程;基本上,您只需装饰类、任何包含的类以及要使用属性序列化的成员。
一旦完成这两件事,您的对象及其 IQueryable 成员就应该可以使用 DataContractJsonSerializer 将其序列化为 JSON。
At my last job, we saw this behavior and it took a bit of doing to get around it.
First, you want the IQ Toolkit, available from CodePlex for free. In its libraries is a "PartialEvaluator" that can reduce the complexity of many expression trees by finding nodes that always evaluate to simpler nodes, and by replacing references to "external closures" with constants. You'll want to run your IQueryable through this before trying to serialize it.
Then, in order to use the JSON DataContract serializer, you must set up the class you wish to serialize as a DataContract. There are tutorials for doing this all over the place; basically you just decorate the class, any contained classes, and the members that you want to serialize with attributes.
Once you have those two things in place, your object and its IQueryable member should be serializable to JSON using the DataContractJsonSerializer.