解决办法有两种:
第一种:使用对象的字段属性设置JsonProperty来实现(不推荐,因为需要手动的修改每个字段的属性)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class UserInfo { [JsonProperty("id")] public int Id{ set; get; } [JsonProperty("userName")] public string UserName{ set; get; } }
第二种:使用newtonsoft.json来设置格式化的方式(推荐使用)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
var user = new { Name = "john", Age = 19 }; var serializerSettings = new JsonSerializerSettings { // 设置为驼峰命名 ContractResolver = new CamelCasePropertyNamesContractResolver() }; var userStr = JsonConvert.SerializeObject(user, Formatting.None, serializerSettings);
配置返回的时间类型数据格式
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); ////配置返回的时间类型数据格式 //GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add( // new Newtonsoft.Json.Converters.IsoDateTimeConverter() // { // DateTimeFormat = "yyyy-MM-dd hh:mm:ss" // }); }