使用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. Sublime Text3插件安装(经典)

    今天我去听数学建模的培训,感觉很有意思,可是我没有报名(QAQ),没有参加培训的报名,不过幸好没有开始选拔比赛 所以我决定学习数学建模方面的知识,要好好学习了! 希望我未来的学弟学妹们!(不要像我这样 ...

  2. java基于BasicPlayer调用 播放音乐

    无聊中想想用java调用下听音乐的api.晚上很多文章用的比较老大方法了,都是用原生的代码写,而且不支持mp3格式,BasicPlayer第三方包提供了很好的api调用,简单的3行代码就可以调用mp3 ...

  3. 单例模式--java代码实现

    单例模式 单例模式,顾名思义,在程序运行中,实例化某个类时只实例化一次,即只有一个实例对象存在.例如在古代,一个国家只能有一个皇帝,在现代则是主席或总统等. 在Java语言中单例模式有以下实现方式 1 ...

  4. 距离度量以及python实现(二)

    接上一篇:http://www.cnblogs.com/denny402/p/7027954.html 7. 夹角余弦(Cosine) 也可以叫余弦相似度. 几何中夹角余弦可用来衡量两个向量方向的差异 ...

  5. Android版数据结构与算法(六):树与二叉树

    版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 之前的篇章主要讲解了数据结构中的线性结构,所谓线性结构就是数据与数据之间是一对一的关系,接下来我们就要进入非线性结构的世界了,主要是树与图,好了接 ...

  6. 我爱Java系列之《JavaEE学习笔记day12》---【缓冲流、转换流、序列/反序列化流、打印流】

    [缓冲流.转换流.序列/反序列化流.打印流] 一.缓冲流 1.字节缓冲输出流 java.io.BufferedOutputStream extends OutputStream 高效字节输出流 写入文 ...

  7. 二级联动,三级联动,初学者,纯javascript,不含jQuery

    二级联动: html代码: <body> <select id="province" onchange="getCity(this.options.se ...

  8. cesium 之地图切换展示效果篇(附源码下载)

    前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...

  9. 300+ Manual Testing and Selenium Interview Questions and Answers

    Manual testing is a logical approach and automation testing complements it. So both are mandatory an ...

  10. 搭建环境-Monkeyrunner-自动化测试工具

    这篇博客帮助挺大,我补充部分,帮助同样的小白哈哈,侵删 https://www.cnblogs.com/lynn-li/p/5885001.html 1.前期准备 需要安装:JDK,SDK,pytho ...