一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

C# XML和JSON 序列化与反序列化

时间:2015-10-31 编辑:简简单单 来源:一聚教程网

1.JSON的序列化与反序列化

    JSON(JavaScript Object Notation)是一种轻量级的数据交换语言,它虽然是JavaScript的一个子集,但它是独立于语言的文本格式。它的数据格式比较简单,易于读写,且都是压缩的,占用带宽小且易于解析,通常会用JSON格式来传输数据或者用于数据存储。有关JSON数据结构语法的更多知识大家可以在网上其它的地方进行了解,我们只要记住在服务端向客户端传输数据时经常使用它,然后对它的序列化和反序列化非常值得了解就ok了。序列化的过程就是把实体类对象转化成JSON字符串对象,直接把实体类的属性名称和属性值组成“名称/值”的格式,反序列化过程则正好相反。(本人渣渣一枚,关于这些比较正式的语言也不太会说,然后有些直接搬的是书上我读得懂的文字咯)

     在这里序列化和反序列化,我们都来学习两种方法,一个是DataContractJsonSerializer(位于System.Runtime.Serialization.Json命名空间下),另一个是JsonObject和JsonArray(位于Windows.Data.Json命名空间下)。下面是具体的演示:

     序列化:

     首先我们定义一个实体类(Student.cs),有Id、Nam、Age三个属性,getTestData()用来返回测试数据。


public  class Student
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }

        public static List getTestData()
        {
            List studentList = new List();
            studentList.Add(new Student() { Id = "201313138063", Name = "czhhhh", Age = 18 });
            studentList.Add(new Student() { Id = "201313138033", Name = "xxxxzh", Age =22 });
            studentList.Add(new Student() { Id = "201313138045", Name = "wwwko", Age = 19 });
            studentList.Add(new Student() { Id = "201313138028", Name = "Marrio", Age = 19 });
            studentList.Add(new Student() { Id = "201313138016", Name = "Mike", Age = 20});
            return studentList;
        }
    }


     然后来进行序列化操作:直接看下面的代码吧(方法一或者二注释掉一个),注释写得很详细。(真废话~~)


 private void InitData()
        {
      
            List studentlist = Student.getTestData();     //取得测试数据

            //序列化Json  方法一:         
           DataContractJsonSerializer seriliazer = new DataContractJsonSerializer(studentlist.GetType());  
             //需要一个Type值,因为我们要序列化的是一个列表数据,直接就拿    
                //学生列表来studentlist.GetType(),这里我们应该了解typeof 和GetType()的区别

            using (MemoryStream ms=new MemoryStream())
            {         
                seriliazer.WriteObject(ms, studentlist);
                   //WriteObject:Serializes a specified object to JSON data
                // and writes the resulting JSON to a stream.(Serializes和writes)

                ms.Position = 0; //注意这个是必须的,因为上一步的操作
                                 //中ms的Position值会指向流的最后,为了后面的读取必须移到开头

                using (StreamReader reader=new StreamReader(ms))  //从流中读取数据
                {
                   tb.Text = reader.ReadToEnd(); //将读取到的数据进行显示
                }
            }

            //序列化Json  方法二:
            JsonArray stujsonarray = new JsonArray();
               //我们要序列化的是一个集合数据(List),所以要借助JsonArray
            //这里JsonArray对应List,而JsonObject则对应Student
           
               foreach (var stu in studentlist)
            {
                JsonObject stujson = new JsonObject();
                stujson.SetNamedValue("id", JsonValue.CreateStringValue(stu.Id));  //name-value 键值对
                stujson.SetNamedValue("name", JsonValue.CreateStringValue(stu.Name));
                stujson.SetNamedValue("age", JsonValue.CreateNumberValue(stu.Age));
                stujsonarray.Add(stujson); //将JsonObject对象添加到JsonArray
            }
            tb.Text = stujsonarray.Stringify();   //调用Stringify()返回封装值的 JSON 表示形式进行显示
        }


     看一下序列化后的结果:


         

        反序列化:

         我们把上面序列化得到的JSON数据保存到一个文件中(Student.txt),然后copy到项目文件夹下来进行反序列化。      


private async void InitDeData()
        {
            //从项目文件夹中读取json到字符串text中
            StorageFolder installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFile storagefile = await installFolder.GetFileAsync("student.txt");
            string text = await FileIO.ReadTextAsync(storagefile);

            List stulist = new List();
       
            //反序列化JSON  方法一
           // DataContractJsonSerializer seriliazer = new DataContractJsonSerializer(typeof(List));
    
              //将json字符串转化为byte []来作为参数实例化MemoryStream
              // using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(text)))  
           // {                                                                       
           //     stulist = seriliazer.ReadObject(ms) as List;
              //     //   Reads a document stream in the JSON (JavaScript Object Notation) format
           //     //   and returns the deserialized object.  (read和return)
           //     //   强制转换成List  
           // }
           //listView.ItemsSource = stulist;  //绑定到ListView控件上进行展示

            //反序列化JSON  方法二
            JsonArray jsonarray = JsonArray.Parse(text); //将JSON字符串转换成JsonArray
                    // (因为我们的JSON字符串是由多个对象组成的数据,单个对象用JsonObjcet.Parse)
            for (int i = 0; i < jsonarray.Count; i++)
            {
                JsonObject jsonobj = jsonarray.GetObjectAt((uint)i);

                // 从键值对中取数据
                stulist.Add(new Student() { Id = jsonobj.GetNamedString("Id"),
                                               Name = jsonobj.GetNamedString("Name"),
                                               Age = (int)jsonobj.GetNamedNumber("Age") });
            }
            listView.ItemsSource = stulist;
        }



           来看反序列化后的结果:

                     

2.XML的序列化与反序列化

     XML(Extensible Markup Language,可扩展标记语言)可用于创建内容,然后使用限定标记进行标记,从而使每个短语、块成为可识别、可分类的信息。它是一种易于使用和扩展的标记语言,它和JSON一样也可以用来进行数据传输和数据存储,但使用比JSON更加广泛,也是一种简单的数据存储格式。有关XML语法更多详细的内容大家可以在网上进行了解。

      和上面JSON的操作相似,XML的序列化与反序列化也采用两种方法来进行,一个是System.Runtime.Serialization命名空间下的DataContractSerializer类,另一个则是Windows.Data.Xml.Dom命名空间下的XmlDocument,具体的操作方法和JSON也是类似的,下面来看演示:

   序列化:

   首先我们还是得定义一个实体类(Book.cs),getTestData返回测试数据。


 public   class Book
    {
        public string Name { get; set; }
        public string Author { get; set; }
        public string Isbn { get; set; }
        public decimal Price { get; set; }
        public static List getTestData()
        {
            List booklist = new List();
            booklist.Add(new Book() { Name = "家", Author = "巴金", Isbn = "9787020058594", Price = 25.00M });
            booklist.Add(new Book() { Name = "希尔伯特几何基础", Author = "希尔伯特", Isbn = "9787301148037", Price = 69.00M });
            booklist.Add(new Book() { Name = "自然哲学之数学原理", Author = "牛顿", Isbn = "9787301095515", Price = 45.00M });
            booklist.Add(new Book() { Name = "卓有成效的管理者", Author = "德鲁克", Isbn = "9787111280712", Price = 55.00M });
            booklist.Add(new Book() { Name = "革命年代", Author = "高华", Isbn = "9787218061948", Price = 35.00M });
            return booklist;
        }
    }



      然后来看具体的序列化操作:注意方法二的规律性(先是books,然后在books里面添加book,book里面有各种属性)


  private async void InitData()
        {
            List booklist = Book.getTestData();

            //    //序列化  方法一:  这个和序列化Json (DataContractJsonSerializer)类似的。
            DataContractSerializer serializer = new DataContractSerializer(booklist.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, booklist);
                ms.Position = 0;
                using (StreamReader reader = new StreamReader(ms))
                {
                    tb.Text = reader.ReadToEnd();
                }
            }

            //    //序列化  方法二:
            //XmlDocument xdoc = new XmlDocument();
            //XmlElement books = xdoc.CreateElement("books");
            //xdoc.AppendChild(books);

            //foreach (var book in booklist)
            //{
            //    XmlElement book1 = xdoc.CreateElement("book");

            //    XmlElement name = xdoc.CreateElement("name");
            //    name.InnerText = book.Name;
            //    book1.AppendChild(name);

            //    XmlElement author = xdoc.CreateElement("author");
            //    author.InnerText = book.Author;
            //    book1.AppendChild(author);

            //    XmlElement Isbn = xdoc.CreateElement("isbn");
            //    Isbn.InnerText = book.Isbn;
            //    book1.AppendChild(Isbn);

            //    XmlElement Price = xdoc.CreateElement("price");
            //    Price.InnerText = book.Price.ToString();
            //    book1.AppendChild(Price);

            //    books.AppendChild(book1);
            //}
     
            //StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("book.xml",
                                 CreationCollisionOption.ReplaceExisting);
            //await  xdoc.SaveToFileAsync(file);

            //if (file != null)
            //{
            //    tb.Text = await FileIO.ReadTextAsync(file);
            //}
        }



       再呢,就是序列化后的结果咯。(我们这里都是内联式的属性,那种key="value"的自行尝试)


     反序列化:

     上面序列化的过程中我们已经将序列化好的数据保存到了xml文件(book.xml)中,下面我们来对这个xml文件进行序列化操作。这里需要注意的是DataContractSerializer类进行反序列化时只能反序列化它自己序列化的数据,也就是上面XmlDocument类序列化之后存储的book.xml是没法用它来反序列化的,我试了很久都是异常,最后才意识到这点。

     方法二看起来代码比较多,其实我们只要掌握了SelectNodes的参数XPath的写法,然后再调试,快速监视一下,根据具体的XML来进行相应的操作就差不多了。这个方法的通用性比较高,XmlDocument系列类也提供了比较多的方法,有必要熟练掌握。


             List booklist = Book.getTestData();

            //  序列化  方法一:  这个和序列化Json (DataContractJsonSerializer)类似的。
            DataContractSerializer serializer = new DataContractSerializer(booklist.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, booklist);
                ms.Position = 0;
                using (StreamReader reader = new StreamReader(ms))
                {
                    tb.Text = reader.ReadToEnd();
                }
            }

            //反序列化XML  方法一       
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(tb.Text)))
            {
                DataContractSerializer deserializer = new DataContractSerializer(typeof(List));
                ms.Position = 0;//这个不要忘记!!!
                booklist = deserializer.ReadObject(ms) as List;
            }
            listView.ItemsSource = booklist;


                //反序列化XML 方法二
                XmlDocument xdoc = new XmlDocument();
                xdoc.LoadXml(text);
                XmlNodeList nodelist = xdoc.SelectNodes("books/book");
                foreach (var node in nodelist)
                {
                    XmlElement xe = (XmlElement)node;
                    booklist.Add(new Book()
                    {
                        Name = xe.ChildNodes.Item(0).InnerText,
                        Author = xe.ChildNodes.Item(1).InnerText,
                        Isbn = xe.ChildNodes.Item(2).InnerText,
                        Price = Convert.ToDecimal(xe.ChildNodes.Item(3).InnerText)
                    });
                }
                listView.ItemsSource = booklist;



下面是方法一的运行结果:

   


这个就是XML和JSON序列化和反序列化的一些内容。

热门栏目