JSON(JavaScript Object Notation)是一種輕量級輕量級的數據交換格式
為了便於下面的演示
[DataContract]
public class User
{
/// <summary>
/// 編號
/// </summary>
[DataMember]
public int UserId { get; set; }
/// <summary>
/// 用戶名
/// </summary>
[DataMember]
public string UserName { get; set; }
/// <summary>
/// 創建時間
/// </summary>
[DataMember]
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime CreateDate { get; set; }
/// <summary>
/// 生日
/// </summary>
[DataMember]
[JsonConverter(typeof(JavaScriptDateTimeConverter))]
public DateTime Birthday { get; set; }
/// <summary>
/// 相關URL
/// </summary>
[DataMember]
public List<string> Urls { get; set; }
/// <summary>
/// 薪水
/// </summary>
//[ScriptIgnore]//使用JavaScriptSerializer序列化時不序列化此字段
//[IgnoreDataMember]//使用DataContractJsonSerializer序列化時不序列化此字段
//[JsonIgnore]//使用JsonConvert序列化時不序列化此字段
public int Salary { get; set; }
/// <summary>
/// 權利級別
/// </summary>
[DataMember]
public Priority Priority { get; set; }
public User()
{
Urls = new List<string>()
}
}
/// <summary>
/// 權利級別
/// </summary>
public enum Priority:byte
{
Lowest=
BelowNormal=
Normal=
AboveNormal=
Highest=
}
使用System
System
下面的代碼是使用JavaScriptSerializer進行序列化和反序列化的例子
public static void JavaScriptSerializerDemo()
{
User user = new User { UserId =
//JavaScriptSerializer類在System
JavaScriptSerializer serializer = new JavaScriptSerializer()
//JSON序列化
string result=serializer
Console
//JSON反序列化
user = serializer
Console
}
說明
使用System
System
下面是使用DataContractJsonSerializer類的例子
public static void DataContractJsonSerializerDemo()
{
User user = new User { UserId =
string result = string
//DataContractJsonSerializer類在System
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(User))
using (MemoryStream stream = new MemoryStream())
{
//JSON序列化
serializer
result = Encoding
Console
}
//JSON反序列化
byte[] buffer = Encoding
using (MemoryStream stream = new MemoryStream(buffer))
{
user = serializer
Console
}
}
注意
使用Newtonsoft
Newtonsoft
下面是使用Newtonsoft
public static void JsonConvertDemo()
{
User user = new User { UserId =
//JsonConvert類在Newtonsoft
//JSON序列化
string result = JsonConvert
Console
//JSON反序列化
user = JsonConvert
Console
}
public static void JsonConvertLinqDemo()
{
User user = new User { UserId =
//JsonConvert類在Newtonsoft
//JSON序列化
string result = JsonConvert
Console
//使用Linq to JSON
JObject jobject = JObject
JToken token = jobject[
List<string> urlList = new List<string>()
foreach (JToken t in token)
{
urlList
}
Console
for (int i =
{
Console
}
Console
}
注意
本文中所有的示例代碼如下
using System;
using System
using System
using System
using System
using System
using System
using System
using Newtonsoft
using Newtonsoft
using Newtonsoft
namespace JSONDemo
{
class Program
{
static void Main(string[] args)
{
JavaScriptSerializerDemo()
DataContractJsonSerializerDemo()
JsonConvertDemo()
JsonConvertLinqDemo()
Console
}
public static void JavaScriptSerializerDemo()
{
User user = new User { UserId =
//JavaScriptSerializer類在System
JavaScriptSerializer serializer = new JavaScriptSerializer()
//JSON序列化
string result=serializer
Console
//JSON反序列化
user = serializer
Console
}
public static void DataContractJsonSerializerDemo()
{
User user = new User { UserId =
string result = string
//DataContractJsonSerializer類在System
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(User))
using (MemoryStream stream = new MemoryStream())
{
//JSON序列化
serializer
result = Encoding
Console
}
//JSON反序列化
byte[] buffer = Encoding
using (MemoryStream stream = new MemoryStream(buffer))
{
user = serializer
Console
}
}
public static void JsonConvertDemo()
{
User user = new User { UserId =
//JsonConvert類在Newtonsoft
//JSON序列化
string result = JsonConvert
Console
//JSON反序列化
user = JsonConvert
Console
}
public static void JsonConvertLinqDemo()
{
User user = new User { UserId =
//JsonConvert類在Newtonsoft
//JSON序列化
string result = JsonConvert
Console
//使用Linq to JSON
JObject jobject = JObject
JToken token = jobject[
List<string> urlList = new List<string>()
foreach (JToken t in token)
{
urlList
}
Console
for (int i =
{
Console
}
Console
}
}
[DataContract]
public class User
{
/// <summary>
/// 編號
/// </summary>
[DataMember]
public int UserId { get; set; }
/// <summary>
/// 用戶名
/// </summary>
[DataMember]
public string UserName { get; set; }
/// <summary>
/// 創建時間
/// </summary>
[DataMember]
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime CreateDate { get; set; }
/// <summary>
/// 生日
/// </summary>
[DataMember]
[JsonConverter(typeof(JavaScriptDateTimeConverter))]
public DateTime Birthday { get; set; }
/// <summary>
/// 相關URL
/// </summary>
[DataMember]
public List<string> Urls { get; set; }
/// <summary>
/// 薪水
/// </summary>
[ScriptIgnore]//使用JavaScriptSerializer序列化時不序列化此字段
[IgnoreDataMember]//使用DataContractJsonSerializer序列化時不序列化此字段
[JsonIgnore]//使用JsonConvert序列化時不序列化此字段
public int Salary { get; set; }
/// <summary>
/// 權利級別
/// </summary>
[DataMember]
public Priority Priority { get; set; }
public User()
{
Urls = new List<string>()
}
}
/// <summary>
/// 權利級別
/// </summary>
public enum Priority:byte
{
Lowest=
BelowNormal=
Normal=
AboveNormal=
Highest=
}
}
程序的運行結果如下
[xhtml]
使用JavaScriptSerializer序列化後的結果
使用JavaScriptSerializer反序列化後的結果
使用DataContractJsonSerializer序列化後的結果
使用DataContractJsonSerializer反序列化後的結果
使用JsonConvert序列化後的結果
使用JsonConvert反序列化後的結果
使用JsonConvert序列化後的結果
使用Linq to JSON反序列化後的結果
總結
From:http://tw.wingwit.com/Article/program/net/201311/13859.html