I have an object that I want to serialize to Json format
I’m using:
public string ToJson()
{
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
string sJSON = jsonSerializer.Serialize(this);
return sJSON;
}
How do I define some fields in “this” to not be serialized?
The possible way is to declare those fields as
privateorinternal.The alternative solution is to use
DataContractJsonSerializerclass. In this case you addDataContractattribute to your class. You can control the members you want to serialize withDataMemberattribute – all members marked with it are serialized, and the others are not.You should rewrite your ToJson method as follows: