• 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();

  • 相关阅读:
    CentOS7 使用定时任务
    springboot整合mybatis+mysql+druid数据源
    js 防抖和节流,封装成函数
    vue 单页面项目卸载依赖和插件的方式
    Tomcat部署vue单页面项目
    linux DRM/KMS 测试工具 modetest、kmscude、igt-gpu-tools (二)
    linux DRM/KMS 测试工具 modetest、kmscude、igt-gpu-tools (一)
    软构设计模式简单归纳
    软件构造复习中关于不变性的一个疑问以及代码验证
    代码快照图与可变不可变
  • 原文地址:https://www.cnblogs.com/yinhaichao/p/2430281.html
Copyright © 2020-2023  润新知