(转)使用.net 3.5的DataContractJsonSerializer进行JSON Serialization
(2011-08-30 18:25:39)
注意:需要引入命名空间
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;(这个.NET Framework 3.5需要添加引用:System.Web.Extensions.dll)
备注:关于另一个json操作类JavaScriptSerializer需要引入命名空间
using System.Web.Script.Serialization;(这个需要添加引用:System.Web.Extensions.dll )
这里举个例子来看,创建一个对象是Person它有FirstName和LastName属性。
- public
class Person - {
-
public Person() { } -
public Person(string firstname, string lastname) -
{ -
this.FirstName = firstname; -
this.LastName = lastname; -
} -
public string FirstName { get; set; } -
public string LastName { get; set; } - }
现在,为了能够让我们的对象通过使用DataContractJsonSerializ
- public
class Person - {
-
public Person() { } -
public Person(string firstname, string lastname) -
{ -
this.FirstName = firstname; -
this.LastName = lastname; -
} -
public string FirstName { get; set; } -
public string LastName { get; set; } -
- }
-
- ///
Marked with the DataContact Attribute - [DataContract]
- public
class Person - {
-
public Person() { } -
public Person(string firstname, string lastname) -
{ -
this.FirstName = firstname; -
this.LastName = lastname; -
} -
-
[DataMember] -
public string FirstName { get; set; } -
-
[DataMember] -
public string LastName { get; set; } - }
下面的代码是实现Person的serialize。
- Person
myPerson = new Person("Chris", "Pietschmann"); -
- ///
Serialize to JSON - System.Runtime.Serialization.Json.DataContractJsonSerializ
er serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializ er(myPerson.GetType()); - MemoryStream
ms = new MemoryStream(); - serializer.WriteObject(ms,
myPerson); - string
json = Encoding.Default.GetString(ms.ToArray());
如果我们使用的Person对象添加了Serializable属性,那么序列化后的结果如下:
/// Result of Person class marked as Serializable
{"<FirstName>k__BackingField":"Chris","<LastName>k__BackingField":"Pietschmann"}
如果是DataContract属性最后结果为:
/// Result of Person class marked as DataContract with
/// each Property marked as DataMember
{"FirstName":"Chris","LastName":"Pietschmann"}
今天做个东西需要用到json序列化,正好用到这个东西。不过熟悉generics的话,使用了generics的帮忙让序列化和反序列化都那么容易了。
贴出他的代码,希望大家能够看懂:
- using
System.Runtime.Serialization; - using
System.Runtime.Serialization.Json; -
- public
class JSONHelper - {
-
public static string Serialize<T>(T obj) -
{ -
System.Runtime.Serialization.Json.DataContractJsonSerializ er serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializ er(obj.GetType()); -
MemoryStream ms = new MemoryStream(); -
serializer.WriteObject(ms, obj); -
string retVal = Encoding.Default.GetString(ms.ToArray()); -
return retVal; -
} -
-
public static T Deserialize<T>(string json) -
{ -
T obj = Activator.CreateInstance<T>(); -
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)); -
System.Runtime.Serialization.Json.DataContractJsonSerializ er serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializ er(obj.GetType()); -
obj = (T)serializer.ReadObject(ms); -
ms.Close(); -
return obj; -
} - }
-
- ///
Our Person object to Serialize/Deserialize to JSON - [DataContract]
- public
class Person - {
-
public Person() { } -
public Person(string firstname, string lastname) -
{ -
this.FirstName = firstname; -
this.LastName = lastname; -
} -
-
[DataMember] -
public string FirstName { get; set; } -
-
[DataMember] -
public string LastName { get; set; } - }
-
-
- ///
Sample code using the above helper methods - ///
to serialize and deserialize the Person object -
- Person
myPerson = new Person("Chris", "Pietschmann"); -
- //
Serialize - string
json = JSONHelper.Serialize<Person>(myPerson); -
- //
Deserialize - myPerson
= JSONHelper.Deserialize<Person>(json);