基本的序列化跟反序列化
1 | using Newtonsoft.Json; |
如果在類別中有某些屬性,要在序列化時跳過的話
1 | using Newtonsoft.Json; |
但如果是類別中只需要少部分指定的屬性要被序列化,也可以這樣子設定
1 | using Newtonsoft.Json; |
跳過為null值1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23public class Movie
{
public string Name { get; set; }
public string Description { get; set; }
public string Classification { get; set; }
public string Studio { get; set; }
public DateTime? ReleaseDate { get; set; }
public List<string> ReleaseCountries { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
Movie movie = new Movie();
movie.Name = "The Best Offer";
movie.Description = "Good Movie";
string ignored = JsonConvert.SerializeObject(movie,
Formatting.Indented,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
}
//Response
//{ Name": "The Best Offer","Description": "Good Movie" }