序列化 XML 和 JSON 的不同成员
我有几个类,使用 DataContract
/ DataMember
属性和 DataContractSerializer
序列化为 XML。 现在我想使用相同的类序列化为 JSON,以供来自客户端的 ajax 调用。 但是,我想要序列化为 JSON 的成员与我想要序列化为 XML 的成员略有不同。有没有办法(带有属性)来区分这两者?
顺便说一句,我使用的是 DataContractSerializer
而不是 XmlSerializer
,因为我需要序列化私有成员,因此不能选择切换到 XmlSerializer
。
I have a couple of classes that I serialize to XML with DataContract
/ DataMember
attributes and the DataContractSerializer
.
Now I would like to use the same classes to serialize to JSON for ajax calls from the client.
However the members that I want to serialize to JSON are slightly different from the ones that I want to serialize to XML. Is there a way (with attributes) to differentiate between these two?
BTW I'm using DataContractSerializer
as opposed to XmlSerializer
because I need to serialize private members, so switching to XmlSerializer
is not an option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于序列化 json,我使用:
您也可以根据您的情况使用它,使用忽略属性来操作输出。
http://msdn.microsoft.com/en -us/library/system.web.script.serialization.scriptignoreattribute.aspx
编辑:
刚刚注意到在处理数据契约时还有另一个您可能更熟悉的选项:
参考:
http://msdn.microsoft.com/en -us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx
http://msdn.microsoft.com/en-us/library/bb412179.aspx
For serializing json, I use:
You could also use it for your situation using the ignore attributes to manipulate the output.
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.scriptignoreattribute.aspx
Edit:
Just noticed there was another option which you might be more familiar with in dealing with datacontracts:
Refersnces:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx
http://msdn.microsoft.com/en-us/library/bb412179.aspx