C# newtonsoft.json 序列化实体进行驼峰命名(第一个首字母小写)

 #使用newtonsoft.json来设置格式化

var Model = new UserIModel { UserName = "xiaoming", UserId = '1' };
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
Console.WriteLine(JsonConvert.SerializeObject(Model , Formatting.Indented, settings));
 

# 设置时间转字符串与小驼峰型 格式

var Model = new UserIModel { UserName = "xiaoming", UserId = '1' ,CreateTime=DateTime.Now};
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
//时间转字符串
timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
IList convertList = new List();
convertList.Add(timeFormat);
var serializerSettings = new JsonSerializerSettings
{
	// 设置为驼峰命名
	ContractResolver = new CamelCasePropertyNamesContractResolver(),
	Converters = convertList
};
context.Response.Write(JsonConvert.SerializeObject(Model, Formatting.Indented, serializerSettings));  

 

你可能感兴趣的:(C#(ASP.Net)随记)