vb.net 教程 4-11 Json文件操作 2 JavaScriptSerializer

这篇博客展示了如何在VB.NET环境下利用JavaScriptSerializer类生成和解析JSON数据。通过按钮事件,实现了从UI界面创建JSON字符串,并将其解析回对应的类实例,强调了属性名与JSON键名的一致性要求。
摘要由CSDN通过智能技术生成

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

使用JavaScriptSerializer生成和解析json:

设计界面:

其中button1和button3是生成,button2和button4是解析。

项目目标框架选择.net Framework 4.5,添加对system.web.extensions的引用,同时代码中添加:

Imports System.Web.Script.Serialization
当button1按下时,生成json格式的字符串:

    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 JavaScriptSerializer
 
        Dim strNewBook As String
        strNewBook = serializer.Serialize(newbooks)
        TextBox1.Text = strNewBook
 
    End Sub
当button2按下时将json字符串解析为类:

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim serializer As New JavaScriptSerializer
        Dim strNewBook As String = TextBox1.Text
 
        Dim newBooks As New Books
        newBooks = serializer.Deserialize(Of Books)(strNewBook)
 
        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
生成和解析结果:


在对json字符串进行解析时,设置的类的属性名称请务必与json代码中的键名称相同。

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 JavaScriptSerializer
 
        Dim strNewBook As String
        strNewBook = serializer.Serialize(newBooks)
        TextBox1.Text = strNewBook
    End Sub
button4解析:

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim serializer As New JavaScriptSerializer
        Dim strNewBook As String = TextBox1.Text
 
        Dim newBooks As New Books1
        newBooks = serializer.Deserialize(Of Books1)(strNewBook)
 
        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版权协议,转载请附上原文出处链接及本声明。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值