主要内容:

JSON的序列化与反序列化

XML的序列化与反序列化

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<Student> getTestData()
{
List<Student> studentList = new List<Student>();
studentList.Add(new Student() { Id = "", Name = "czhhhh", Age = });
studentList.Add(new Student() { Id = "", Name = "xxxxzh", Age = });
studentList.Add(new Student() { Id = "", Name = "wwwko", Age = });
studentList.Add(new Student() { Id = "", Name = "Marrio", Age = });
studentList.Add(new Student() { Id = "", Name = "Mike", Age = });
return studentList;
}
}

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

      private void InitData()
{ List<Student> 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 = ; //注意这个是必须的,因为上一步的操作
//中ms的Position值会指向流的最后,为了后面的读取必须移到开头 using (StreamReader reader=new StreamReader(ms)) //从流中读取数据
{
tb.Text = reader.ReadToEnd(); //将读取到的数据进行显示
}
} //序列化Json 方法二:
JsonArray stujsonarray = new JsonArray();
//我们要序列化的是一个集合数据(List<Student>),所以要借助JsonArray
//这里JsonArray对应List<Student>,而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<Student> stulist = new List<Student>(); //反序列化JSON 方法一
// DataContractJsonSerializer seriliazer = new DataContractJsonSerializer(typeof(List<Student>));

//将json字符串转化为byte []来作为参数实例化MemoryStream
// using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(text)))
// {
// stulist = seriliazer.ReadObject(ms) as List<Student>;
// // Reads a document stream in the JSON (JavaScript Object Notation) format
// // and returns the deserialized object. (read和return)
// // 强制转换成List<Student>
// }
//listView.ItemsSource = stulist; //绑定到ListView控件上进行展示 //反序列化JSON 方法二
JsonArray jsonarray = JsonArray.Parse(text); //将JSON字符串转换成JsonArray
// (因为我们的JSON字符串是由多个对象组成的数据,单个对象用JsonObjcet.Parse)
for (int i = ; 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<Book> getTestData()
{
List<Book> booklist = new List<Book>();
booklist.Add(new Book() { Name = "家", Author = "巴金", Isbn = "", Price = 25.00M });
booklist.Add(new Book() { Name = "希尔伯特几何基础", Author = "希尔伯特", Isbn = "", Price = 69.00M });
booklist.Add(new Book() { Name = "自然哲学之数学原理", Author = "牛顿", Isbn = "", Price = 45.00M });
booklist.Add(new Book() { Name = "卓有成效的管理者", Author = "德鲁克", Isbn = "", Price = 55.00M });
booklist.Add(new Book() { Name = "革命年代", Author = "高华", Isbn = "", Price = 35.00M });
return booklist;
}
}

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

  private async void InitData()
{
List<Book> booklist = Book.getTestData(); // //序列化 方法一: 这个和序列化Json (DataContractJsonSerializer)类似的。
DataContractSerializer serializer = new DataContractSerializer(booklist.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, booklist);
ms.Position = ;
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<Book> booklist = Book.getTestData();

            //  序列化  方法一:  这个和序列化Json (DataContractJsonSerializer)类似的。
DataContractSerializer serializer = new DataContractSerializer(booklist.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, booklist);
ms.Position = ;
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<Book>));
ms.Position = ;//这个不要忘记!!!
booklist = deserializer.ReadObject(ms) as List<Book>;
}
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;

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

ok,这个就是XML和JSON序列化和反序列化的一些内容了,XML这部分开始不是这样子的,Microsoft Edge在我快写完的时候卡的一下把我下午写的恢复到了昨天晚上的状态!!!  下次见!

最新文章

  1. jquery简介和实例
  2. powershell中使用超大内存对象
  3. java word 转 pdf
  4. 向ArcGIS的ToolBarControl中添加任意的windows控件的方法
  5. cpu中断
  6. Struts2常规配置
  7. Python自动化运维之10、模块之json、pickle、XML、PyYAML、configparser、shutil
  8. Struts2学习笔记--Struts例子及开发流程
  9. Working with Strings(使用Oracle字符串)
  10. web中使用扫描枪
  11. 跟我一起学extjs5(16--各种Grid列的自己定义渲染)
  12. dapper 扩展插件: Rainbow
  13. 间隔Ns请求某函数并且有timeout
  14. SQL 判断 ‘表,存储过程,函数 ...’ 已是否存在
  15. 7、Web应用程序中的安全向量 -- 使用Retail部署配置
  16. MYSQL超时连接问题(com.mysql.jdbc.MysqlIO.readFully)
  17. AR入门系列-02-Vuforia在Unity3d编辑器的使用
  18. Atlas框架介绍集成(一)
  19. 详解vue生命周期
  20. [Windows] 重新安装/卸载桌面版OneDrive / Reinstall/ Uninstall Desktop Version OneDrive

热门文章

  1. php课程 4-16 数组自定义函数(php数组-&gt;桶)
  2. HDU2473 Junk-Mail Filter - 并查集删除操作(虚父节点)
  3. c头文件(.h)的作用
  4. c++ try catch 问题(没有throw就会被删除,加上/EHa这样就不会被删除)
  5. 病毒软件,导致DNS失效的解决办法
  6. Java数组课后习题
  7. 【u235】背单词
  8. 【BZOJ 1029】[JSOI2007]建筑抢修
  9. 视频和音频播放的演示最简单的例子9:SDL2广播PCM
  10. leetcode先刷_Valid Sudoku