.net3.5提供了json对象序列化与反序列化的类。位置在:System.Runtime.Serialization.Json空间下。其中如果要应用这个空间还必须添加对
System.ServiceModel
System.ServiceModel.Web
这两个库文件的引用。
参考实体类:Customer
public
class
Customer
{
public int Unid { get ; set ; }
public string CustomerName { get ; set ; }
}
{
public int Unid { get ; set ; }
public string CustomerName { get ; set ; }
}
DataContractJsonSerializer
将对象序列化为 JavaScript 对象表示法 (JSON),并将 JSON 数据反序列化为对象。无法继承此类。
其中有个方法WriteObject,它的功能定义为:将对象序列化为 JavaScript 对象表示法 (JSON) 文档
它有三个方法重载,其中一个为:
public
override
void
WriteObject(Stream stream,Object graph)
它的功能描述这:将指定对象序列化为 JavaScript 对象表示法 (JSON) 数据,并将生成的 JSON 写入流中
(一)序列化
public
string
ToJson(Customer customer)
{
DataContractJsonSerializer ds = new DataContractJsonSerializer( typeof (Customer));
MemoryStream ms = new MemoryStream();
ds.WriteObject(ms, customer);
string strReturn = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return strReturn;
}
{
DataContractJsonSerializer ds = new DataContractJsonSerializer( typeof (Customer));
MemoryStream ms = new MemoryStream();
ds.WriteObject(ms, customer);
string strReturn = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return strReturn;
}
创建类实例,通过它的WriteObject方法来向流写入序列化的对象,再把流写入到字符串中。就可以得到JSON对象。
测试一下:
Customer cc
=
new
Customer {Unid
=
1
,CustomerName
=
"
John
"
};
string strJson = ToJson(cc);
Console.WriteLine(strJson);
string strJson = ToJson(cc);
Console.WriteLine(strJson);
结果为:{"CustomerName":"John","Unid":1}
(二)反序列化
ReadObject方法,其描述为:反序列化 JSON(JavaScript 对象表示法)数据,并返回反序列化的对象。
它有很多重载,现在通过一种:
public override Object ReadObject(Stream stream)
它从流中得到反序列化的对象。
public
object
FromJson(
string
strJson)
{
DataContractJsonSerializer ds = new DataContractJsonSerializer( typeof (Customer));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson));
return ds.ReadObject(ms);
}
{
DataContractJsonSerializer ds = new DataContractJsonSerializer( typeof (Customer));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson));
return ds.ReadObject(ms);
}
测试:
string
strJson
=
"
{\
"
CustomerName\
"
:\
"
John\
"
,\
"
Unid\
"
:1}
"
;
Customer c = FromJson(strJson) as Customer;
Console.WriteLine(c.Unid + " " + c.CustomerName);
Customer c = FromJson(strJson) as Customer;
Console.WriteLine(c.Unid + " " + c.CustomerName);
(三)通过泛型方法对两者进行修改
为了适应多类型实例的序列化与反序列化,通过泛型方法来实现。
public
string
ToJson
<
T
>
(T t)
{
DataContractJsonSerializer ds = new DataContractJsonSerializer( typeof (T));
MemoryStream ms = new MemoryStream();
ds.WriteObject(ms, t);
string strReturn = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return strReturn;
}
public T FromJson < T > ( string strJson) where T: class
{
DataContractJsonSerializer ds = new DataContractJsonSerializer( typeof (T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson));
return ds.ReadObject(ms) as T;
}
{
DataContractJsonSerializer ds = new DataContractJsonSerializer( typeof (T));
MemoryStream ms = new MemoryStream();
ds.WriteObject(ms, t);
string strReturn = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return strReturn;
}
public T FromJson < T > ( string strJson) where T: class
{
DataContractJsonSerializer ds = new DataContractJsonSerializer( typeof (T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson));
return ds.ReadObject(ms) as T;
}
· 反序列化时通过泛型约束来指定类型 T 为类类型。
测试:
Customer cc
=
new
Customer {Unid
=
1
,CustomerName
=
"
John
"
};
string strJsons = ToJson < Customer > (cc);
Console.WriteLine(strJsons);
string strJson = " {\ " CustomerName\ " :\ " John\ " ,\ " Unid\ " :1} " ;
Customer c = FromJson < Customer > (strJson);
Console.WriteLine(c.Unid + " " + c.CustomerName);
string strJsons = ToJson < Customer > (cc);
Console.WriteLine(strJsons);
string strJson = " {\ " CustomerName\ " :\ " John\ " ,\ " Unid\ " :1} " ;
Customer c = FromJson < Customer > (strJson);
Console.WriteLine(c.Unid + " " + c.CustomerName);