標籤:
一、 VS2008以上版本
/// <summary>
/// 序列
/// </summary>
/// <typeparam name="T">對象類</typeparam>
/// <param name="t">類對象</param>
/// <returns>JSON字串</returns>
public static string JsonSerializer<T>(T t)
{
// 將對象序列化為 JavaScript 物件標記法 (JSON),並將 JSON 資料還原序列化為對象。
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
// 建立其支援儲存區為記憶體的流
MemoryStream ms = new MemoryStream();
// 將指定對象序列化為 JavaScript 物件標記法 (JSON) 資料,並將產生的 JSON 寫入流中
ser.WriteObject(ms, t);
// 寫入位元組數群組轉換字串
string tojson = Encoding.UTF8.GetString(ms.ToArray());
// 關閉流
ms.Close();
// 返回JSON字元
return tojson;
}
/// <summary>
/// 還原序列化
/// </summary>
/// <typeparam name="T">對象類</typeparam>
/// <param name="strJson">要反序列的JSON字串</param>
/// <returns>返回對象</returns>
public static T JsonDeserializer<T>(string strJson)
{
// 將對象序列化為 JavaScript 物件標記法 (JSON),並將 JSON 資料還原序列化為對象
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
// 建立其支援儲存區為記憶體的流
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson));
// 以 JSON(JavaScript 物件標記法)格式讀取文檔流,並返回還原序列化的對象。
T obj = (T)ser.ReadObject(ms);
// 返回
return obj;
}
調用方法:
private void button1_Click(object sender, EventArgs e)
{
student s = new student();
List<student> stu = new List<student>();
s.age = 11;
s.height = 12;
s.weight = 10;
s.name = "tao";
stu.Add(s);
student s2 = new student();
s2.age = 21;
s2.height = 22;
s2.weight = 20;
s2.name = "jian";
stu.Add(s2);
string json = JsonHelper.JsonSerializer < List<student>>(stu);
listBox1.Items.Add(json);
textBox1.Text = json;
}
private void button2_Click(object sender, EventArgs e)
{
List<student> s = JsonHelper.JsonDeserializer<List<student>>(textBox1.Text);
listBox1.Items.Add(s[0].name);
}
private void button4_Click(object sender, EventArgs e)
{
student s = new student();
s.age = 11;
s.height = 12;
s.weight = 10;
s.name = "tao222";
string json = JsonHelper.JsonSerializer<student>(s);
listBox1.Items.Add(json);
textBox1.Text = json;
}
private void button5_Click(object sender, EventArgs e)
{
student s = JsonHelper.JsonDeserializer<student>(textBox1.Text);
listBox1.Items.Add(s.name);
}
private void button3_Click(object sender, EventArgs e)
{
DataTable dt = getDataTable();
string json = JsonHelper.JsonSerializer(dt);
listBox1.Items.Add(json);
textBox1.Text = json;
}
private void button6_Click(object sender, EventArgs e)
{
DataTable dt = JsonHelper.JsonDeserializer<DataTable>(textBox1.Text);
listBox1.Items.Add(dt.Rows[0]["name"]);
}
二 、 VS2005 版本 網上有個封裝好的第三方很方便
private void button1_Click(object sender, EventArgs e)
{
student s = new student();
List<student> stu = new List<student>();
s.age = 11;
s.height = 12;
s.weight = 10;
s.name = "tao";
stu.Add(s);
student s2 = new student();
s2.age = 21;
s2.height = 22;
s2.weight = 20;
s2.name = "jian";
stu.Add(s2);
string json = JsonConvert.SerializeObject(stu);
listBox1.Items.Add(json);
textBox1.Text = json;
}
private void button2_Click(object sender, EventArgs e)
{
List<student> s = JsonConvert.DeserializeObject<List<student>>(textBox1.Text);
listBox1.Items.Add(s[0].name);
}
private void button3_Click(object sender, EventArgs e)
{
student s = new student();
s.age = 11;
s.height = 12;
s.weight = 10;
s.name = "tao";
string json = JsonConvert.SerializeObject(s);
listBox1.Items.Add(json);
textBox1.Text = json;
}
private void button4_Click(object sender, EventArgs e)
{
student s = JsonConvert.DeserializeObject<student>(textBox1.Text);
listBox1.Items.Add(s.age);
}
private void button5_Click(object sender, EventArgs e)
{
DataTable dt = getDataTable();
string json = JsonConvert.SerializeObject(dt);
listBox1.Items.Add(json);
textBox1.Text = json;
}
private void button6_Click(object sender, EventArgs e)
{
DataTable dt = JsonConvert.DeserializeObject<DataTable>(textBox1.Text);
listBox1.Items.Add(dt.Rows[0]["name"]);
}
C#裡 JSON 序列化 與 還原序列化