通过asp.net 3.5自带的类来序列化和反序列化Json。

有两种方式:

1.通过DataContractJsonSerializer类实现Json

为了使用这个类实现Json序列化,需要引用System.ServiceModel.Web和System.Runtime.Serialization

using System.Runtime.Serialization.Json;

属性说明:

DataMember:定义序列化属性参数(使用DataMember属性必须使用DataContract属性标记类,否则无效)

IgnoreDataMember:定义成员不被序列化

例子:

序列化

    [DataContract]
    public class Student
    {
        [DataMember]
        public int Id;

        [DataMember(Name = "UserName")]
        public string Name;

        [IgnoreDataMember]
        public int Age;
    }
    
    static void Main(string[] args)
    {        
        Student student = new Student
        {
            Id = 1,
            Name = "yxg",
            Age = 30
        };

        Console.WriteLine(student.ToJsJson());
            
        Console.Read();
    }
结果:

jieguo

反序列化

    static void Main(string[] args)
    {       
        string s_student = "{\"Id\":1,\"UserName\":\"yxg\",\"Age\":30}";

        Student student = s_student.FromJsonTo<Student>();

        Console.WriteLine(string.Format("Id:{0},Name:{1},Age:{2}", student.Id, student.Name, student.Age));
            
        Console.Read();
    }

结果:

json2

以上操作通过使用下面的封装类实现:

    public static class JsonHelper
    {
        /// <summary>
        /// Json序列化,用于发送到客户端
        /// </summary>
        public static string ToJsJson(this object item)
        {
            
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType());

            using (MemoryStream ms = new MemoryStream())
            {

                serializer.WriteObject(ms, item);

                StringBuilder sb = new StringBuilder();

                sb.Append(Encoding.UTF8.GetString(ms.ToArray()));

                return sb.ToString();
            }
        }

        /// <summary>
        /// Json反序列化,用于接收客户端Json后生成对应的对象
        /// </summary>
        public static T FromJsonTo<T>(this string jsonString)
        {

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));

            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));

            T jsonObject = (T)ser.ReadObject(ms);

            ms.Close();

            return jsonObject;

        }

    }

2.通过JavaScriptSerializer类来实现Json

为了使用这个类实现Json序列化,需要引用System.ServiceModel.Web和

System.Runtime.Serialization和System.Web.Extensions

 

using System.Web.Script.Serialization;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;

属性说明:

ScriptIgnore:定义成员不被序列化

例子:

序列化

    public class Student
    {

        public int Id;

        public string Name;

        [ScriptIgnore]
        public int Age;
    }

    static void Main(string[] args)
    {
        JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();

        Student student = new Student
        {
            Id = 1,
            Name = "yxg",
            Age = 30
        };

        Console.WriteLine(jsonSerializer.Serialize(student));

        Console.Read();
    }

 

结果:

json3

反序列化

    static void Main(string[] args)
    {
        string s_student = "{\"Id\":1,\"UserName\":\"yxg\",\"Age\":30}";

        JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();

        Student student = jsonSerializer.Deserialize<Student>(s_student);

        Console.WriteLine(string.Format("Id:{0},Name:{1},Age:{2}", student.Id, student.Name, student.Age));

        Console.Read();
    }

 

结果:

json4

 

这个类操作简单,不用单独封装。