序列化和反序列化解释
- 序列化:把对象转换为字节序列的过程称为对象的序列化.
- 反序列化:把字节序列恢复为对象的过程称为对象的反序列化.
综上序列化的主要目的是通过网络传输对象或者说是将对象存储到文件系统、数据库、内存中。
效果图;
1,下面是vb.net 实现代码form
Imports System.Web.Script.Serialization ' 添加此行以引入 JavaScriptSerializer 类所属的命名空间
Imports System.Collections ' 使用ArrayList引用
'Imports System.Collections.Generic
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' 创建DataTable对象
Dim dt As New DataTable()
' 定义列名及数据类型
dt.Columns.Add("ID", GetType(Integer))
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Age", GetType(Integer))
' 添加新行到DataTable
dt.Rows.Add(1, "张三", 25)
dt.Rows.Add(2, "李四", 30)
dt.Rows.Add(3, "王五", 28)
TextBox1.Text = SerializeDataTable(dt)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = DeserializerTable(TextBox1.Text)
End Sub
End Class
*****************************vb.net 实现函数**************************************
'1,引用中添加的是System.Web.Extensions引用
'2,using System.Web.Script.Serialization; 添加此行以引入 JavaScriptSerializer 类所属的命名空间
'3,using System.Collections; ' 使用ArrayList引用
' <summary>
' DataTable序列化
' </summary>
' <param name="dt"></param>
' <returns></returns>
Public Shared Function SerializeDataTable(ByVal dt As DataTable) As String
Dim serializer As JavaScriptSerializer = New JavaScriptSerializer
Dim list As New List(Of Dictionary(Of String, Object))()
For Each dr As DataRow In dt.Rows
Dim result As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
For Each dc As DataColumn In dt.Columns '每一行信息,新建一个Dictionary<string,object>,将该行的每列信息加入到字典
result.Add(dc.ColumnName, dr(dc).ToString)
Next
list.Add(result)
Next
Return serializer.Serialize(list) '调用Serializer方法
End Function
' <summary>
' DataTable反序列化
' </summary>
' <param name="strJson"></param>
' <returns></returns>
Public Shared Function DeserializerTable(ByVal strJson As String) As DataTable
Dim dt As DataTable = New DataTable
Dim serializer As JavaScriptSerializer = New JavaScriptSerializer
Dim arralList As ArrayList = serializer.Deserialize(Of ArrayList)(strJson) '反序列化ArrayList类型
If (arralList.Count > 0) Then '反序列化后ArrayList个数不为0
For Each row As Dictionary(Of String, Object) In arralList
If (dt.Columns.Count = 0) Then '新建的DataTable中无任何信息,为其添加列名及类型
For Each key As String In row.Keys
dt.Columns.Add(key, row(key).GetType)
'添加dt的列名
Next
End If
Dim dr As DataRow = dt.NewRow
For Each key As String In row.Keys '讲arrayList中的值添加到DataTable中
dr(key) = row(key)
'添加列值
Next
dt.Rows.Add(dr)
'添加一行
Next
End If
Return dt
End Function
********************************以下是c#实现函数*************************************************
//1,引用中添加的是System.Web.Extensions引用
//2,using System.Web.Script.Serialization; 添加此行以引入 JavaScriptSerializer 类所属的命名空间
//3,using System.Collections; // 使用ArrayList引用
/// <summary>
/// DataTable序列化
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string SerializeDataTable(DataTable dt)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)//每一行信息,新建一个Dictionary<string,object>,将该行的每列信息加入到字典
{
Dictionary<string, object> result = new Dictionary<string, object>();
foreach (DataColumn dc in dt.Columns)
{
result.Add(dc.ColumnName, dr[dc].ToString());
}
list.Add(result);
}
return serializer.Serialize(list);//调用Serializer方法
}
/// <summary>
/// DataTable反序列化
/// </summary>
/// <param name="strJson"></param>
/// <returns></returns>
public static DataTable DeserializerTable(string strJson)
{
DataTable dt = new DataTable();
JavaScriptSerializer serializer = new JavaScriptSerializer();
ArrayList arralList = serializer.Deserialize<ArrayList>(strJson);//反序列化ArrayList类型
if (arralList.Count > 0)//反序列化后ArrayList个数不为0
{
foreach (Dictionary<string, object> row in arralList)
{
if (dt.Columns.Count == 0)//新建的DataTable中无任何信息,为其添加列名及类型
{
foreach (string key in row.Keys)
{
dt.Columns.Add(key, row[key].GetType());//添加dt的列名
}
}
DataRow dr = dt.NewRow();
foreach (string key in row.Keys)//讲arrayList中的值添加到DataTable中
{
dr[key] = row[key];//添加列值
}
dt.Rows.Add(dr);//添加一行
}
}
return dt;
}