使用JsonProperty Attribute修改返回 json 值的name

本例使用JsonPropertyAttribute在序列化为JSON时更改属性的名称。

public class Videogame
{
[JsonProperty("name")]
public string Name { get; set; } [JsonProperty("release_date")]
public DateTime ReleaseDate { get; set; }
}
Videogame starcraft = new Videogame
{
Name = "Starcraft",
ReleaseDate = new DateTime(1998, 1, 1)
}; string json = JsonConvert.SerializeObject(starcraft, Formatting.Indented); Console.WriteLine(json);
// {
// "name": "Starcraft",
// "release_date": "1998-01-01T00:00:00"
// }

排序

public class Account
{
public string EmailAddress { get; set; } // appear last
[JsonProperty(Order = 1)]
public bool Deleted { get; set; } [JsonProperty(Order = 2)]
public DateTime DeletedDate { get; set; } public DateTime CreatedDate { get; set; }
public DateTime UpdatedDate { get; set; } // appear first
[JsonProperty(Order = -2)]
public string FullName { get; set; }
}
Account account = new Account
{
FullName = "Aaron Account",
EmailAddress = "aaron@example.com",
Deleted = true,
DeletedDate = new DateTime(2013, 1, 25),
UpdatedDate = new DateTime(2013, 1, 25),
CreatedDate = new DateTime(2010, 10, 1)
}; string json = JsonConvert.SerializeObject(account, Formatting.Indented); Console.WriteLine(json);
// {
// "FullName": "Aaron Account",
// "EmailAddress": "aaron@example.com",
// "CreatedDate": "2010-10-01T00:00:00",
// "UpdatedDate": "2013-01-25T00:00:00",
// "Deleted": true,
// "DeletedDate": "2013-01-25T00:00:00"
// }

在反序列化期间使用的Required,以验证是否存在所需的JSON属性

public class Videogame
{
[JsonProperty(Required = Required.Always)]
public string Name { get; set; } [JsonProperty(Required = Required.AllowNull)]
public DateTime? ReleaseDate { get; set; }
}
string json = @"{
'Name': 'Starcraft III',
'ReleaseDate': null
}"; Videogame starcraft = JsonConvert.DeserializeObject<Videogame>(json); Console.WriteLine(starcraft.Name);
// Starcraft III Console.WriteLine(starcraft.ReleaseDate);
// null

JsonIgnoreAttribute

使用JsonIgnoreAttribute从序列化中排除属性

public class Account
{
public string FullName { get; set; }
public string EmailAddress { get; set; } [JsonIgnore]
public string PasswordHash { get; set; }
}

详情请参考 https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm

使用JsonProperty Attribute修改返回json的更多相关文章

  1. 修改 mvc webapi 默认返回 json 格式

    web api 默认的已 xml 格式返回数据 现在开发一般都是以 json 格式为主 下面配置让 webapi 默认返回 json ,在需要返回 xml 时只需要加一个查询参数 datatype=x ...

  2. Mui.ajax请求服务器正确返回json数据格式

    ajax: mui.ajax('http://server-name/login.php',{ data:{ username:'username', password:'password' }, d ...

  3. Struts2返回json格式数据踩坑记录

    事件起因 昨天提测修改冻结/解冻银行卡样式的功能,微姐测试过程中发现调用ajax请求耗时过长,今天来排查,发现浏览器请求/finance/ajax/freeze/ajaxGetShopLists时,对 ...

  4. Web API返回JSON数据

    对Web API新手来说,不要忽略了ApiController 在web API中,方法的返回值如果是实体的话实际上是自动返回JSON数据的例如: 他的返回值就是这样的: { "Conten ...

  5. web Api 返回json 的两种方式

    web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...

  6. webapi返回json格式优化

    一.设置webapi返回json格式 在App_Start下的WebApiConfig的注册函数Register中添加下面这代码 config.Formatters.Remove(config.For ...

  7. [转]SpringMVC使用@ResponseBody时返回json的日期格式、@DatetimeFormat使用注意

    一.SpringMVC使用@ResponseBody时返回json的日期格式 前提了解: @ResponseBody 返回json字符串的核心类是org.springframework.http.co ...

  8. struts2 的验证框架validation如何返回json数据 以方便ajax交互

    struts2 的验证框架validation简单,好用,但是input只能输出到jsp页面通过struts2的标签<s:fielderror  />才能取出,(EL应该也可以). 如果使 ...

  9. webapi返回json格式,并定义日期解析格式

    1.webapi返回json格式 var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferen ...

随机推荐

  1. ISCC2018(最新的考核解析)

    最近一直在做这个 ISCC2018,感觉可能自己只是一个新手吧!但是我会继续努力的,希望我的解题思路能够给你们带来一定的想法,我也希望自己能够在安全方面遇到更多志同道合的人! 其它题目可以看这里 1 ...

  2. 计算机17-3,4作业A

    A货车过隧道问题 Description 输入若干组数据,每组数据中有三个整数分别表示某条公路沿途所经过的三个隧道的最大高度,数之间用单个空格分隔.输入高度单位是厘米,范围在0到762之间.现有一台高 ...

  3. SSH(Spring4+Struts2+Hibernate4)框架整合

    1.加入Spring4 ①. 加入 jar 包

  4. HashMap 实现及原理

    1.为什么用HashMap? HashMap是一个散列桶(数组和链表),它存储的内容是键值对(key-value)映射HashMap采用了数组和链表的数据结构,能在查询和修改方便继承了数组的线性查找和 ...

  5. CAP带你轻松玩转Asp.Net Core消息队列

    CAP是什么? CAP是由我们园子里的杨晓东大神开发出来的一套分布式事务的决绝方案,是.Net Core Community中的第一个千星项目(目前已经1656 Star),具有轻量级.易使用.高性能 ...

  6. Wmyskxz文章目录导航附Java精品学习资料

    前言:这段时间一直在准备校招的东西,所以一晃眼都好长时间没更新了,这段时间准备的稍微好那么一点点,还是觉得准备归准备,该有的学习节奏还是要有..趁着复习的空隙来整理整理自己写过的文章吧..好多加了微信 ...

  7. 一个小实例理解js 原型和继承

    导语1:一个构造函数的原型对象,其实就是这个构造函数的一个属性而已,属性名叫prototype,值是一个对象,对象中有一些属性和方法,所以每个构造函数的实例对象都拥有这些属性和方法的使用权. 导语2: ...

  8. 8天入门docker系列 —— 第五天 使用aspnetcore小案例熟悉容器互联和docker-compose一键部署

    这一篇继续完善webnotebook,如果你读过上一篇的内容,你应该知道怎么去挂载webnotebook日志和容器的远程访问,但是这些还远不够,webnotebook 总要和一些数据库打交道吧,比如说 ...

  9. Git使用详细教程(8):Git分支

    目录 创建分支 查看分支 切换分支 删除分支 分支合并 探寻分支本质 创建分支 当我们使用git init projectName命令的时候,Git就会默认帮我们创建一个分支,名字叫做master. ...

  10. Postgresql数据库部署之:Postgresql本机启动和Postgresql注册成windows 服务

    1.初始化并创建数据库(一次即可)  initdb \data --locale=chs -U postgres -W  You can now start the database server u ...