對JSON數據進行序列化和反序列化
可以使用DataContractJsonSerializer類將類型實例序列化為JSON字符串
方法一
{
public int Id { get; set; }
public string Name { get; set; }
}
執行序列化反序列化
protected void Page_Load(object sender
{
Personnel personnel = new Personnel();
personnel
personnel
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
//執行序列化
string r
//執行反序列化
Personnel _Personnel = jsonSerializer
}
r
可以使用 ScriptIgnore屬性標記不序列化公共屬性或公共字段
{
[ScriptIgnore]
public int Id { get; set; }
public string Name { get; set; }
}
r
方法二
序列化類
public class People
{
public int Id { get; set; }
public string Name { get; set; }
}
執行序列化反序列化
代碼如下 protected void Page_Load(object sender
{
People people = new People();
people
people
DataContractJsonSerializer json = new DataContractJsonSerializer(people
string szJson = "";
//序列化
using (MemoryStream stream = new MemoryStream())
{
json
szJson = Encoding
}
//反序列化
using (MemoryStream ms = new MemoryStream(Encoding
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(People));
People _people = (People)serializer
}
}
szJson輸出結果
可以使用IgnoreDataMember:指定該成員不是數據協定的一部分且沒有進行序列化
[DataContract]
public class People
{
[DataMember(Name = "id")]
public int Id { get; set; }
[IgnoreDataMember]
public string Name { get; set; }
}
輸出結果: {"id":
JSON序列化和反序列化日期時間的處理
JSON格式不直接支持日期和時間
修改Person類
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime LastLoginTime { get; set; }
}
Person p = new Person();
p
p
p
string jsonString = JsonHelper
序列化結果
{"Age":
using System;
using System
using System
using System
using System
using System
using System
using System
/// <summary>
/// JSON序列化和反序列化輔助類
/// </summary>
public class JsonHelper
{
/// <summary>
/// JSON序列化
/// </summary>
public static string JsonSerializer<T>(T t)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
ser
string jsonString = Encoding
ms
//替換Json的Date字符串
string p = @"\/Date((d+)+d+)\/";
MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
Regex reg = new Regex(p);
jsonString = reg
return jsonString;
}
/// <summary>
/// JSON反序列化
/// </summary>
public static T JsonDeserialize<T>(string jsonString)
{
//將"yyyy
string p = @"d{
MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertDateStringToJsonDate);
Regex reg = new Regex(p);
jsonString = reg
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding
T obj = (T)ser
return obj;
}
/// <summary>
/// 將Json序列化的時間由/Date(
/// </summary>
private static string ConvertJsonDateToDateString(Match m)
{
string result = string
DateTime dt = new DateTime(
dt = dt
dt = dt
result = dt
return result;
}
/// <summary>
/// 將時間字符串轉為Json時間
/// </summary>
private static string ConvertDateStringToJsonDate(Match m)
{
string result = string
DateTime dt = DateTime
dt = dt
TimeSpan ts = dt
result = string
return result;
}
}
序列化Demo
p
p
p
string jsonString = JsonHelper
運行結果
反序列化Demo
string json = "{"Age":
p=JsonHelper
From:http://tw.wingwit.com/Article/program/net/201311/14467.html