.NET 3.5: 使用DataContractJsonSerializer进行JSON 序列化
来源:互联网 发布:录音软件aa ce 编辑:程序博客网 时间:2024/05/23 18:33
In ASP.NET AJAX Extensions v1.0 for ASP.NET 2.0 there is the JavaScriptSerializer class that provides JSON serialization and deserialization functionality. However, in .NET 3.5 the JavaScriptSerializer has been marked obsolete. The new object to use for JSON serialization in .NET 3.5 is the DataContractJsonSerliaizer object. I'm still new to the DataContractJsonSerializer, but here's a summary of what I've learned so far...
Object to Serialize
Here's a simple Person object with First Name and Last Name properties.
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; }
}
Now in order to serialize our object to JSON using the DataContractJsonSerializer we must either mark it with the Serializable attribute or the DataContract attribute. If we mark the class with the DataContract attribute, then we must mark each property we want serialized with the DataMember attribute.
/// Marked with the Serializable Attribute
[Serializable]
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; }
}
Serialization Code
Jere's the most basic code to serialize our object to JSON:
Person myPerson = new Person("Chris", "Pietschmann");
/// Serialize to JSON
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(myPerson.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, myPerson);
string json = Encoding.Default.GetString(ms.ToArray());
Our resulting JSON looks like this:
/// Result of Person class marked as Serializable
{"<FirstName>k__BackingField":"Chris","<LastName>k__BackingField":"Pietschmann"}
/// Result of Person class marked as DataContract with
/// each Property marked as DataMember
{"FirstName":"Chris","LastName":"Pietschmann"}
As you can see the first serialization with the class marked with the Serializable attribute isn't quite what we were expecting, but is still JSON. This serialization actually isn't compatible with the client-side JSON Serializer in ASP.NET AJAX.
As you can see the second serialization with the class marked with the DataContract attribute is exactly what we were expecting, and is the same JSON that the old JavaScriptSerializer object would have generated. This is the method of JSON serialization using the DataContractJsonSerializer that you'll need to do if you are going to pass the resulting JSON down to the client to be consumed with ASP.NET AJAX.
Deserialization Code
Here's the most basic code to deserialize our object from JSON:
Person myPerson = new Person();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(myPerson.GetType());
myPerson = serializer.ReadObject(ms) as Person;
ms.Close();
Controlling the property names in the resulting JSON
When using the DataContract and DataMember attributes, you can tell the DataMember attribute the specific name you want that property to have within the JSON serialization by setting its "Name" named parameter.
Here's an example that will give the name of "First" to the "FirstName" property within the JSON serialization:
[DataMember(Name = "First")]
public string FirstName { get; set; }
The resulting JSON looks like this:
{"First":"Chris","LastName":"Pietschmann"}
Here's the code for some Helper methods using Generics to do all the dirty work for you
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
public class JSONHelper
{
public static string Serialize<T>(T obj)
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(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.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(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);
What Assembly References does my application need for this?
From the namespace that contains DataContractJsonSerializer you can probably tell that you need to add a reference to the System.Runtime.Serialization assembly. However, you also need to add a reference to the System.ServiceModel.Web assembly.
- .NET 3.5: 使用DataContractJsonSerializer进行JSON 序列化
- 使用.net 3.5的DataContractJsonSerializer进行JSON Serialization
- 使用.net 3.5的DataContractJsonSerializer进行JSON Serialization
- 使用.net 3.5的DataContractJsonSerializer进行JSON Serialization
- .net 3.5 DataContractJsonSerializer序列化及反序列化二(json格式化)
- DataContractJsonSerializer进行Json的序列化和反序列化
- .Net 3.5 用DataContractJsonSerializer实现Json序列和反序列
- .Net——使用DataContractJsonSerializer进行序列化及反序列化基本操作
- .Net——使用DataContractJsonSerializer进行序列化及反序列化基本操作
- 使用DataContractJsonSerializer进行序列化和反序列化
- Net 3.5 用DataContractJsonSerializer实现Json序列和反序列例子
- Net 3.5 用DataContractJsonSerializer实现Json序列和反序列例子
- Net 3.5 用DataContractJsonSerializer实现Json序列和反序列例子
- 解决如下json格式的字符串不能使用DataContractJsonSerializer序列化和反序列化
- 关于C#使用DataContractJsonSerializer来进行JSON解析
- C# 中使用JSON - DataContractJsonSerializer
- C# 中使用JSON - DataContractJsonSerializer
- C# 中使用JSON - DataContractJsonSerializer
- Ext智能提示 - Spket(Eclipse插件)
- 将实体类或者集合序列化为JSON字符串,将JSON字符串反序列化还原为对象
- DB2数据库三种级别的字符集的设置与修改
- PI 实时数据库系统
- setjmp函数功能简介
- .NET 3.5: 使用DataContractJsonSerializer进行JSON 序列化
- 多变行的碰撞
- maven2使用手册
- 多变行的碰撞
- Java 删除文件和目录代码
- 基于ARM的嵌入式系统Bootloader启动流程分析[转自http://blog.ednchina.com/darkbluexn/11951/message.aspx#]
- video4linux sink详解
- Java八荣八耻
- 十年总结(13):开发不息,重构不止