官網參考:http://json.codeplex.com/
在程序包管理控制台,鍵入NuGet命令 install-package Newtonsoft.Json 安裝Newtonsoft.Json
PM> install-package Newtonsoft.Json
正在安裝“Newtonsoft.Json 6.0.5”。
已成功安裝“Newtonsoft.Json 6.0.5”。
正在將“Newtonsoft.Json 6.0.5”添加到 MVCDemo.Model。
已成功將“Newtonsoft.Json 6.0.5”添加到 MVCDemo.Model。
原文:The quickest method of converting between JSON text and a .NET object is using the JsonSerializer. The JsonSerializer converts .NET objects into their JSON equivalent and back again by mapping the .NET object property names to the JSON property names and copies the values for you.
譯文:最快的JSON和文本之間的轉換方法。NET對象是使用jsonserializer。該jsonserializer轉換成JSON等效.NET對象 通過.NET對象屬性名稱映射成JSON屬性名稱,並且給你屬性對應的Value值。(翻譯水平太差,勿噴)
JsonConvert
原文:For simple scenarios where you want to convert to and from a JSON string the SerializeObject() and DeserializeObject() methods on JsonConvert provide an easy to use wrapper over JsonSerializer.
翻譯:對於簡單的情況下,包裝在jsonserialize中的JsonConvert,他的serializeobject()和deserializeobject()方法,讓對象和Json字符串之間的轉換變的更加簡單(翻譯水平太差,勿噴)
用法:
Product product = new Product(); product.Name = "Apple"; product.ExpiryDate = new DateTime(2008, 12, 28); product.Price = 3.99M; product.Sizes = new string[] { "Small", "Medium", "Large" }; 8string output = JsonConvert.SerializeObject(product); //{ // "Name": "Apple", // "ExpiryDate": "2008-12-28T00:00:00", // "Price": 3.99, // "Sizes": [ // "Small", // "Medium", // "Large" // ] //} Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);
原文: SerializeObject and DeserializeObject both have overloads that take a JsonSerializerSettings object. JsonSerializerSettings lets you use many of the JsonSerializer settings listed below while still using the simple serialization methods.
譯文:SerializeObject和DeserializeObject 都有帶有JsonSerializerSettings 對象的重載方法,JsonSerializerSettings 讓你使用下面列出的許多jsonserializer設置時仍然使用簡單的序列化方法。(翻譯水平太差,勿噴)
//轉換成Json字符串形式,返回格式:[{...},{...},{...}] public static string ToJson(this object value, string defaultValue = "\"\"") { if (value==null) { return defaultValue; } else { var json= JsonConvert.SerializeObject(value);//序列化 return json.Equals("null") ? defaultValue : json; } }