Json.Net: 官方說是給.NET使用高效能的Json Framework.
主要功能:
- Dynamic to JSON format: 利用.NET 4.0 dynamic動態定義與JSON之間轉換.
- Class to JSON format: 將Class與JSON之間自動進行轉換.
- LINQ to JSON format: 不需事先定義class即可,直接建立、讀取或修改JSON資料.
Dynamic to JSON format:
dynamic product = new JObject();product.ProductName = "Elbow Grease";product.Enabled = true;product.Price = 4.90m;product.StockCount = 9000;product.StockValue = 44100; // All Elbow Grease must go sale!// 50% off price product.Price = product.Price / 2;product.StockValue = product.StockCount * product.Price;product.ProductName = product.ProductName + " (SALE)"; string json = product.ToString();// {// "ProductName": "Elbow Grease (SALE)",// "Enabled": true,// "Price": 2.45,// "StockCount": 9000,// "StockValue": 22050.0// }Class to JSON format:
public class Message{ public string Address { get; set; } [JsonProperty(TypeNameHandling = TypeNameHandling.All)]
public object Body { get; set; }
} public class SearchDetails{public string Query { get; set; }
public string Language { get; set; }
}Message message = new Message();message.Address = "http://www.google.com";message.Body = new SearchDetails {Query = "Json.NET",
Language = "en-us"
}; string json = JsonConvert.SerializeObject(message, Formatting.Indented);// {// "Address": "http://www.google.com",
// "Body": {
// "$type": "Newtonsoft.Json.Tests.Serialization.SearchDetails, Newtonsoft.Json.Tests",
// "Query": "Json.NET",
// "Language": "en-us"
// }
// } Message deserialized = JsonConvert.DeserializeObject<Message>(json); SearchDetails searchDetails = (SearchDetails) deserialized.Body; LINQ to JSON format:
JArray array = new JArray();JValue text = new JValue("Manual text");JValue date = new JValue(new DateTime(2000, 5, 23)); array.Add(text);array.Add(date); string json = array.ToString();// [// "Manual text",// "\/Date(958996800000+1200)\/"// ]List<Post> posts = GetPosts(); JObject rss = new JObject( new JProperty("channel", new JObject( new JProperty("title", "James Newton-King"), new JProperty("link", "http://james.newtonking.com"), new JProperty("description", "James Newton-King's blog."), new JProperty("item", new JArray( from p in posts orderby p.Title select new JObject( new JProperty("title", p.Title), new JProperty("description", p.Description), new JProperty("link", p.Link), new JProperty("category", new JArray( from c in p.Categories select new JValue(c))))))))); Console.WriteLine(rss.ToString()); //{// "channel": {// "title": "James Newton-King",// "link": "http://james.newtonking.com",// "description": "James Newton-King's blog.",// "item": [// {// "title": "Json.NET 1.3 + New license + Now on CodePlex",// "description": "Annoucing the release of Json.NET 1.3, the MIT license and the source being available on CodePlex",// "link": "http://james.newtonking.com/projects/json-net.aspx",// "category": [// "Json.NET",// "CodePlex"// ]// },// {// "title": "LINQ to JSON beta",// "description": "Annoucing LINQ to JSON",// "link": "http://james.newtonking.com/projects/json-net.aspx",// "category": [// "Json.NET",// "LINQ"// ]// }// ]// }//}在ASP.NET與Javascript之間進行ajax資料傳遞的時候,JSON格式算是一個很便利的傳輸格式,且jQuery有直接取得JSON format的method,對於前端資料顯示的處理便利很多.
沒有留言:
張貼留言