If you want to change serialization behavior in Newtonsoft.Json aka JSON.NET, you need to create your settings:
var jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore // ignore null values
});
You can also pass these settings into JsonConvert.SerializeObject
:
JsonConvert.SerializeObject(objectToSerialize, serializerSettings);
For ASP.NET MVC and Web API. In Global.asax:
protected void Application_Start()
{
GlobalConfiguration.Configuration
.Formatters
.JsonFormatter
.SerializerSettings
.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
Exclude null values:
GlobalConfiguration.Configuration
.Formatters
.JsonFormatter
.SerializerSettings
.NullValueHandling = NullValueHandling.Ignore;
Indicates that null values should not be included in resulting JSON.
ASP.NET Core
ASP.NET Core by default serializes values in camelCase format.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…