#使用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<JsonConverter> convertList = new List<JsonConverter>();
convertList.Add(timeFormat);
var serializerSettings = new JsonSerializerSettings
{
// 設置爲駝峯命名
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = convertList
};
context.Response.Write(JsonConvert.SerializeObject(Model, Formatting.Indented, serializerSettings));