Questions tagged «json-serialization»

8
如何将C#匿名类型序列化为JSON字符串?
我正在尝试使用以下代码将匿名类型序列化为JSON: var serializer = new DataContractJsonSerializer(thing.GetType()); var ms = new MemoryStream(); serializer.WriteObject(ms, thing); var json = Encoding.Default.GetString(ms.ToArray()); 但是,执行此操作时,出现以下异常: 类型'<> f__AnonymousType1`3 [System.Int32,System.Int32,System.Object []]'无法序列化。考虑使用DataContractAttribute属性标记它,并使用DataMemberAttribute属性标记要序列化的所有成员。有关其他受支持的类型,请参见Microsoft .NET Framework文档。 据我所知,我不能将属性应用于匿名类型。是否有另一种方法来执行此序列化,或者我缺少什么?

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.