-
vb.net 教程 4-11 Json文件操作 3 DataContractJsonSerializer
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
设计界面和控件功能同上一节,只是换了DataContractJsonSerializer来生成和解析json。
使用.net framework 4.5框架,不用专门再去引用system.web.extensions,直接在代码中
Imports System.Runtime.Serialization.Json
由于还要使用到内存流,所以还需要:
Imports System.IO
button1生成:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newbooks As New Books
Dim newBook As New BookStore
Dim newSingleBook As New Book
newSingleBook.Title = "这是测试"
newSingleBook.Author = "作者"
newSingleBook.Country = "中国"
newSingleBook.Price = 10.2
newSingleBook.Year = "1985"
newBook.book = newSingleBook
newbooks.bookstore = newBook
Dim serializer As New DataContractJsonSerializer(GetType(Books))
Dim ms As New MemoryStream()
serializer.WriteObject(ms, newbooks)
Dim json() As Byte
json = ms.ToArray
ms.Close()
Dim strNewBook As String = System.Text.Encoding.UTF8.GetString(json)
TextBox1.Text = strNewBook
End Sub
button2解析:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strNewBook As String = TextBox1.Text
Dim newbooks As New Books
Dim serializer As New DataContractJsonSerializer(GetType(Books))
Dim bteNewBook() As Byte
bteNewBook = System.Text.Encoding.UTF8.GetBytes(strNewBook)
Dim ms As MemoryStream
ms = New MemoryStream(bteNewBook) '
newbooks = serializer.ReadObject(ms)
TextBox2.Text &= "---------------" & ControlChars.CrLf
TextBox2.Text &= "Title:" & newbooks.bookstore.book.Title & ControlChars.CrLf
TextBox2.Text &= "Author:" & newbooks.bookstore.book.Author & ControlChars.CrLf
TextBox2.Text &= "Country:" & newbooks.bookstore.book.Country & ControlChars.CrLf
TextBox2.Text &= "Price:" & newbooks.bookstore.book.Price & ControlChars.CrLf
TextBox2.Text &= "Year:" & newbooks.bookstore.book.Year & ControlChars.CrLf
End Sub
button3生成:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim newBooks As New Books1
ReDim newBooks.bookstore(2)
Dim newSingleBook As New Book
newSingleBook.Title = "这是测试"
newSingleBook.Author = "作者"
newSingleBook.Country = "中国"
newSingleBook.Price = 10
newSingleBook.Year = "1985"
Dim newBook As New BookStore1
newBook.book = newSingleBook
newBooks.bookstore(0) = newBook
newSingleBook = New Book
newSingleBook.Title = "这是测试1"
newSingleBook.Author = "作者"
newSingleBook.Country = "中国"
newSingleBook.Price = 11
newSingleBook.Year = "1985"
newBook = New BookStore1
newBook.book = newSingleBook
newBooks.bookstore(1) = newBook
newSingleBook = New Book
newSingleBook.Title = "这是测试2"
newSingleBook.Author = "作者"
newSingleBook.Country = "中国"
newSingleBook.Price = 12
newSingleBook.Year = "1985"
newBook = New BookStore1
newBook.book = newSingleBook
newBooks.bookstore(2) = newBook
Dim serializer As New DataContractJsonSerializer(GetType(Books1))
Dim ms As New MemoryStream()
serializer.WriteObject(ms, newBooks)
Dim json() As Byte
json = ms.ToArray
ms.Close()
Dim strNewBook As String = System.Text.Encoding.UTF8.GetString(json)
TextBox1.Text = strNewBook
End Sub
button4解析:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim strNewBook As String = TextBox1.Text
Dim newbooks As New Books1
Dim serializer As New DataContractJsonSerializer(GetType(Books1))
Dim bteNewBook() As Byte
bteNewBook = System.Text.Encoding.UTF8.GetBytes(strNewBook)
Dim ms As MemoryStream
ms = New MemoryStream(bteNewBook) '
newbooks = serializer.ReadObject(ms)
For i As Integer = 0 To newbooks.bookstore.Length - 1
TextBox2.Text &= "---------------" & ControlChars.CrLf
TextBox2.Text &= "Title:" & newbooks.bookstore(i).book.Title & ControlChars.CrLf
TextBox2.Text &= "Author:" & newbooks.bookstore(i).book.Author & ControlChars.CrLf
TextBox2.Text &= "Country:" & newbooks.bookstore(i).book.Country & ControlChars.CrLf
TextBox2.Text &= "Price:" & newbooks.bookstore(i).book.Price & ControlChars.CrLf
TextBox2.Text &= "Year:" & newbooks.bookstore(i).book.Year & ControlChars.CrLf
Next
End Sub
生成的结果同上一节。
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看vb.net 教程 目录
————————————————
版权声明:本文为CSDN博主「VB.Net」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/uruseibest/article/details/79243953
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比