Json序列化和反序列化的几种方法

首先先定义一个Person类,用于序列化和反序列化

    public class Person
      {
          string name;

          public string Name
          {
              get { return name; }
              set { name = value; }
          }
          DateTime time;

          public DateTime Time
          {
              get { return time; }
              set { time = value; }
          }

      }

     //方法1(使用DataContractJsonSerializey方法)
            //必须首先引用命名空间(using System.Runtime.Serialization.Json;来自引用程序集System.ServiceModel.Web),还要引用命名空间(using System.Runtime.Serialization来自引用程序集System.Runtime.Serialization)
            //因为DataContractJsonSerializey继承了XmlObjectSerializer类,所以必须要引用System.Runtime.Serialization命名空间
            //序列化(普通数据)
            Person person = new Person();
            person.Name = "殷海超";
            person.Time = Convert.ToDateTime("2012-01-01");
            MemoryStream ms = new MemoryStream();//定义内存流
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person));//初始化 DataContractJsonSerializer 类的新实例,以便序列化或反序列化指定类型的对象(这里是Person类型)
            ser.WriteObject(ms, person);//使用DataContractJsonSerializer 的WriteObject方法将Person写入流

            ms.Position = 0;//让流的初始位置位于0
            StreamReader sr = new StreamReader(ms);//这是把写入流的序列化好的数据读取出来,供查看
            Console.WriteLine(sr.ReadToEnd());


            //反序列化(普通数据)
            ms.Position = 0;
            Person personread = (Person)ser.ReadObject(ms);//使用DataContractJsonSerializer 的ReadObject方法将流ms读取出来反序列化为Person类型的数据
            Console.WriteLine("{0}{1}", personread.Name, personread.Time);//输出

            Console.ReadLine();

    //序列化(List<T>)
            List<Person> list = new List<Person>();
            list.Add(new Person() { Name = "殷海超", Time = Convert.ToDateTime("2012-01-01") });
            list.Add(new Person() { Name = "王琪", Time = Convert.ToDateTime("2012-01-01") });

    //或者List<Person> list = new List<Person>(){
      new Person() { Name = "殷海超", Time = Convert.ToDateTime("2012-01-01") },
      new Person() { Name = "王琪", Time = Convert.ToDateTime("2012-01-01") }
     }
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Person>));//(这里是List<Person>类型)
            ser.WriteObject(ms, list);

            ms.Position = 0;
            StreamReader sr = new StreamReader(ms);
            Console.WriteLine(sr.ReadToEnd());
            //反序列化(List<T>)
            ms.Position = 0;
            List<Person> personread = (List<Person>)ser.ReadObject(ms);
            Console.WriteLine("{0}{1}", personread[0].Name, personread[0].Time);
            Console.WriteLine("{0}{1}", personread[1].Name, personread[1].Time);
            Console.ReadLine();

     //方法2(使用JavaScriptSerializer类)
            //使用JavaScriptSerializer类,引用命名空间是using System.Web.Script.Serialization;添加引用程序集System.Web.Extensions(在 System.Web.Extensions.dll 中)    

    //序列化
     Person person = new Person();
            person.Name = "殷海超";
            person.Time = Convert.ToDateTime("2012-01-01");
            JavaScriptSerializer js = new JavaScriptSerializer();
            string strJSON = js.Serialize(person);
            Console.WriteLine(strJSON);

    //反序列化
            Person t = js.Deserialize<Person>(strJSON);
            Console.WriteLine(t.Name + t.Time);
            Console.ReadLine();


            //方法3使用Newtonsoft.Json.dll
   
//下载地址http://json.codeplex.com 要引用using Newtonsoft.Json;using Newtonsoft.Json.Converters;

    //序列化
            Person person = new Person();
            person.Name = "殷海超";
            person.Time = Convert.ToDateTime("2012-01-01");
            string strJSON = JsonConvert.SerializeObject(person);
            Console.WriteLine(strJSON);

    //反序列化
            Person t = JsonConvert.DeserializeObject<Person>("{Name:\"殷海超\",Time:\"2012-03-07\"}");
            Console.WriteLine(t.Name + t.Time);
            Console.ReadLine();

posted @ 2012-04-02 20:46  蹦极的馒头  阅读(1460)  评论(1编辑  收藏  举报