使用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. 【毕业】-《伯恩茅斯大学毕业证书》BU一模一样原件

    ☞伯恩茅斯大学毕业证书[微/Q:2544033233◆WeChat:CC6669834]UC毕业证书/联系人Alice[查看点击百度快照查看][留信网学历认证&博士&硕士&海归 ...

  2. 本地和svn都删除文件导致版本不同的问题

    想要删除一个项目中的文件,同是要删除svn上的文件. 自己操作 1.直接右键删除了本地项目中的一个目录的模块 2.右键删除了库中svn中的这个目录 3.同步本地和svn上的代码 4.问题出现了,本地和 ...

  3. SPPNET

    SPPNet Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition 文章地址:https://ar ...

  4. java游戏开发杂谈 - 游戏物体

    现实生活中,有很多物体,每个物体的长相.行为都不同. 物体存在于不同的空间内,它只在这个空间内发生作用. 物体没用了,空间就把它剔除,不然既占地方,又需要花精力管理. 需要它的时候,就把它造出来,不需 ...

  5. Python爬取南京市往年天气预报,使用pyecharts进行分析

    上一次分享了使用matplotlib对爬取的豆瓣书籍排行榜进行分析,但是发现python本身自带的这个绘图分析库还是有一些局限,绘图不够美观等,在网上搜索了一波,发现现在有很多的支持python的绘图 ...

  6. 【公告】关于8.8MIP组件审核平台故障的说明

    故障通报 2017 年 8 月 8 日 下午 14:11,由于机器故障原因,MIP 组件管理平台暂时无法提供服务. 2017 年 8 月 8 日 下午 16:46, 故障已解决.MIP 组件管理平台服 ...

  7. FreeSql 新查询功能介绍

    FreeSql FreeSql 是一个功能强大的 NETStandard 库,用于对象关系映射程序(O/RM),提供了 CodeFirst/DbFirst/CURD/表达式函数/读写分离 等基础封装. ...

  8. [翻译 EF Core in Action 1.10] 应该在项目中使用EF Core吗?

    Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...

  9. Android-----Intent中通过startActivity(Intent intent )隐式启动新的Activity

    显式Intent我已经简单使用过了,也介绍过概念,现在来说一说隐式Intent: 隐式Intent:就是只在Intent中设置要进行的动作,可以用setAction()和setData()来填入要执行 ...

  10. python3 购物车

    今天干了啥?喂了喂龟,看了看鱼... 然后就是学习了两个模块:sys模块和os模块,突然觉得python真的好,只要英语学的好,看代码超级舒服的说,嗯,我要好好学英语,今天背了几个啥,唉.写完博客再背 ...