Json.net元件介紹 20130726
Upcoming SlideShare
Loading in...5
×
 

Json.net元件介紹 20130726

on

  • 374 views

 

Statistics

Views

Total Views
374
Views on SlideShare
374
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Json.net元件介紹 20130726 Json.net元件介紹 20130726 Presentation Transcript

  • 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 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 View slide
  • 常用的json.Net元件 • JsonConvert • JObject • JArray View slide
  • 範例實作 • 有一個定義好的物件: Public class product { Public string name { get; set; } public DateTime expiry { get; set; } public decimal price { get; set; } public string[] sizes { get; set; } }
  • 範例實作 • 將定義好的物件轉成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);
  • 範例實作 • 將剛才的json string轉成指定的物件: Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
  • 範例實作 • 示範LINQ to JSON的範例: string json = @"{ ""Name"": ""Apple"", ""Expiry"": "2008-12-28T00:00:00", ""Price"": 3.99, ""Sizes"": [ ""Small"", ""Medium"", ""Large"" ] }";
  • 範例實作 • 使用JObject讀取JSON字串中的某一個property JObject o = JObject.Parse(json); string name = (string)o["Name"]; • 或是下面的方式也可以: string name = o.Value<string>("Name");
  • 範例實作 • 使用JArray讀取JSON字串中的某一個array property JArray sizes = (JArray)o["Sizes"]; string smallest = (string)sizes[0];
  • 參考資料 HTTP://JAMES.NEWTONKING.COM/PROJECTS/JSON-NET.ASPX