Json.Net元件介紹
日期:2013/07/26
演說者:林政融
Agenda
• 為什麼使用Json.Net
• 常用的Json.Net元件
• JsonConvert
• JObject
• JArray
• 範例實作
為什麼使用Json.Net
• Flexible JSON serializer for converting between .NET objects and JSON
• LINQ to JSON for manually reading ...
常用的json.Net元件
• JsonConvert
• JObject
• JArray
範例實作
• 有一個定義好的物件:
Public class product
{
Public string name { get; set; }
public DateTime expiry { get; set; }
public deci...
範例實作
• 將定義好的物件轉成json string:
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, ...
範例實作
• 將剛才的json string轉成指定的物件:
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
範例實作
• 示範LINQ to JSON的範例:
string json = @"{
""Name"": ""Apple"",
""Expiry"": "2008-12-28T00:00:00",
""Price"": 3.99,
""Siz...
範例實作
• 使用JObject讀取JSON字串中的某一個property
JObject o = JObject.Parse(json);
string name = (string)o["Name"];
• 或是下面的方式也可以:
stri...
範例實作
• 使用JArray讀取JSON字串中的某一個array property
JArray sizes = (JArray)o["Sizes"];
string smallest = (string)sizes[0];
參考資料
HTTP://JAMES.NEWTONKING.COM/PROJECTS/JSON-NET.ASPX
Upcoming SlideShare
Loading in …5
×

Json.net元件介紹 20130726

270 views
201 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
270
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Json.net元件介紹 20130726

  1. 1. Json.Net元件介紹 日期:2013/07/26 演說者:林政融
  2. 2. Agenda • 為什麼使用Json.Net • 常用的Json.Net元件 • JsonConvert • JObject • JArray • 範例實作
  3. 3. 為什麼使用Json.Net • Flexible JSON serializer for converting between .NET objects and JSON • LINQ to JSON for manually reading and writing JSON • High performance, faster than .NET's built-in JSON serializers • Write indented, easy to read JSON • Convert JSON to and from XML • Supports .NET 2, .NET 3.5, .NET 4, Silverlight and Windows Phone
  4. 4. 常用的json.Net元件 • JsonConvert • JObject • JArray
  5. 5. 範例實作 • 有一個定義好的物件: Public class product { Public string name { get; set; } public DateTime expiry { get; set; } public decimal price { get; set; } public string[] sizes { get; set; } }
  6. 6. 範例實作 • 將定義好的物件轉成json string: Product product = new Product(); product.Name = "Apple"; product.Expiry = new DateTime(2008, 12, 28); product.Price = 3.99M; product.Sizes = new string[] { "Small", "Medium", "Large" }; string json = JsonConvert.SerializeObject(product);
  7. 7. 範例實作 • 將剛才的json string轉成指定的物件: Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
  8. 8. 範例實作 • 示範LINQ to JSON的範例: string json = @"{ ""Name"": ""Apple"", ""Expiry"": "2008-12-28T00:00:00", ""Price"": 3.99, ""Sizes"": [ ""Small"", ""Medium"", ""Large"" ] }";
  9. 9. 範例實作 • 使用JObject讀取JSON字串中的某一個property JObject o = JObject.Parse(json); string name = (string)o["Name"]; • 或是下面的方式也可以: string name = o.Value<string>("Name");
  10. 10. 範例實作 • 使用JArray讀取JSON字串中的某一個array property JArray sizes = (JArray)o["Sizes"]; string smallest = (string)sizes[0];
  11. 11. 參考資料 HTTP://JAMES.NEWTONKING.COM/PROJECTS/JSON-NET.ASPX

×