SlideShare una empresa de Scribd logo
1 de 11
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
常用的json.Net元件
• JsonConvert
• JObject
• JArray
範例實作
• 有一個定義好的物件:
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

Más contenido relacionado

Más de LearningTech

Más de LearningTech (20)

PostCss
PostCssPostCss
PostCss
 
ReactJs
ReactJsReactJs
ReactJs
 
Docker
DockerDocker
Docker
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
 
node.js errors
node.js errorsnode.js errors
node.js errors
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
 
Expression tree
Expression treeExpression tree
Expression tree
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
 
flexbox report
flexbox reportflexbox report
flexbox report
 
Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
 
Reflection &amp; activator
Reflection &amp; activatorReflection &amp; activator
Reflection &amp; activator
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
 
Node child process
Node child processNode child process
Node child process
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
 
Expression tree
Expression treeExpression tree
Expression tree
 
Vic weekly learning_20160325
Vic weekly learning_20160325Vic weekly learning_20160325
Vic weekly learning_20160325
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
git command
git commandgit command
git command
 
Asp.net MVC DI
Asp.net MVC DIAsp.net MVC DI
Asp.net MVC DI
 

Json.net元件介紹 20130726