问题
How can I change the way Newtonsoft JSON.NET serializes property names of objects?
回答1:
A couple of ways:
- You can manually control how it serializes using the
JsonTextWriter
class:- http://james.newtonking.com/projects/json/help/index.html?topic=html/ReadingWritingJSON.htm
- You could implement a custom
JsonConverter
that does what you want:- http://james.newtonking.com/projects/json/help/index.html?topic=html/T_Newtonsoft_Json_JsonConverter.htm
- Example: JSON.NET customizing the serialization to exclude a property name
回答2:
You can create a model with the property names. And change them by creating some private variables that will be use to as return values for the properties. This is will direct the deserializer to reset the name of the property.
private int _privateId;
public int NameThatExistAlreadyInTheJson
{
set { _privateId = value; }
}
public int NameYouWantItToBeDisplayInstead
{
get { return _privateId; }
}
来源:https://stackoverflow.com/questions/13091862/change-the-way-json-net-serializes-property-names