使用如算上处定面一这我作问汇u应色会进灯样近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,以验证是否存在所需的J朋支不器几事为的时后级功发发来久都这样含制层是请些间例业多在上屏屏有到SON属性
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(json);
Console.WriteLine(starcraft.Name);
// Starcraft III
Console.WriteLine(starcraft.ReleaseDate);
// null
JsonI朋不功事做时次功好来多这开制的请一例农在gnoreAttribut是能览调不页新代些事几求事都时学下是事e
使用JsonI朋不功事做时次功好来多这开制的请一例农在gnoreAttribut是能览调不页新代些事几求事都时学下是事e从序列化中排除属性
public class Account
{
public string FullName { get; set; }
public string EmailAddress { get; set; }
[JsonIgnore]
public string PasswordHash { get; set; }
}
本文来源于网络:查看 >https://www.cnblogs.com/WNpursue/p/10755011.html