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.
For serializing json, I use:
System.Web.Script.Serialization.JavaScriptSerializer
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:
System.Runtime.Serialization.Json.DataContractJsonSerializer
Refersnces:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.