ASP.NET - JSON 相關筆記

C# 與 Json 相關的筆記

基本的序列化跟反序列化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

//建立類別
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}

//基本的 序列化輸出模樣 + 反序列化
protected void Page_Load(object sender, EventArgs e)
{
//給值
Person myPerson = new Person();
myPerson.Name = "Stoenr";
myPerson.Age = 18;

//序列化
// Formatting.Indented 會生成良好的顯示格式
// Formatting.None會跳過不必要的空格和換行符號,讓Json的結果更小。生成的顯示格式更加緊湊,也許效率更高。
string output = JsonConvert.SerializeObject(myPerson, Formatting.Indented);
Response.Write(output);

//反序列化
Person book2 = JsonConvert.DeserializeObject<Person>(output);
string output2 = string.Format("<br/>Name:{0},Age:{1}", book2.Name, book2.Age);
Response.Write(output2);

//Json 查詢
JObject myQuery = JsonConvert.DeserializeObject<JObject>(output);
string output3 = string.Format("<br/>Query:{0}", myQuery.GetValue("Name")); //myQuery["Name"]
Response.Write(output3);
}

//Response output:
//{"Name":"Stoenr","Age":18}

//Response output2:
//Name:Stoenr,Age:18

如果在類別中有某些屬性,要在序列化時跳過的話

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;

public enum Options
{
Option1, Option2
}

public class Entity
{
public Options A1 { get; set; }
public Options A2 { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public Options B1 { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public Options B2 { get; set; }
public string P1 { get; set; }
[JsonIgnore]
public string P2 { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
Entity myEntity = new Entity()
{
A1 = Options.Option1,
A2 = Options.Option2,
B1 = Options.Option1,
B2 = Options.Option2,
P1 = "P1",
P2 = "P2"
};
string output4 = JsonConvert.SerializeObject(myEntity, Formatting.Indented);
Response.Write("<br/>" + output4);
}

//Response output4
//{ "A1": 0, "A2": 1, "B1": "Option1", "B2": "Option2", "P1": "P1" }

但如果是類別中只需要少部分指定的屬性要被序列化,也可以這樣子設定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Runtime.Serialization;

[DataContract]
public class Computer
{
//序列化
[DataMember]
public string Name { get; set; }
[DataMember]
public decimal SalePrice { get; set; }

//忽略
public string Manufacture { get; set; }
public int StockCount { get; set; }
public decimal WholeSalePrice { get; set; }
public DateTime NextShipmentDate { get; set; }
}

跳過為null值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public 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" }

0%