方法一、
单独忽略某个属性。使用
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
引用
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public IList<RequestpassengerInfo> passenger { get; set; }
/// <summary>
/// 价格信息节点
/// </summary>
public RequestpriceInfo price { get; set; }
/// <summary>
/// 儿童价格信息节点
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public RequestchdPriceInfo chdPrice { get; set; }
方法二、
全局忽略。使用
NullValueHandling = NullValueHandling.Ignore
调用JsonConvert.SerializeObject()时使用NullValueHandling.Ignore
string json = JsonConvert.SerializeObject(
Attributes,
Formatting.Indented,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
本文介绍两种在JSON序列化中忽略空值的方法:一是通过属性装饰器[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]单独忽略特定属性;二是通过JsonSerializerSettings设置NullValueHandling=NullValueHandling.Ignore全局忽略所有空值。
163

被折叠的 条评论
为什么被折叠?



