Vb.net序列化和反序列化json之JavaScriptSerializer对象

很多都是用Newtonsoft.Json,这次记录一下.net framework自带的方法。JavaScriptSerializer对象,需要先引用System.Web.Extensions.dll

Imports System.Web.Script.Serialization

Public Class _Default
    Inherits Page

Public Class Person  'peson类
    Public Property PersonID As Integer
    Public Property Name As String
    Public Property Registered As Boolean
End Class



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim RegisteredUsers As New List(Of Person)()
        RegisteredUsers.Add(New Person With {.PersonID = 1, .Name = "Bryon Hetrick", .Registered = True})
        RegisteredUsers.Add(New Person With {.PersonID = 2, .Name = "Nicole Wilcox", .Registered = True})
        RegisteredUsers.Add(New Person With {.PersonID = 3, .Name = "Adrian Martinson", .Registered = False})
        RegisteredUsers.Add(New Person With {.PersonID = 4, .Name = "Nora Osborn", .Registered = False})

        Dim serializer As New JavaScriptSerializer()
        Dim serializedResult = serializer.Serialize(RegisteredUsers)
        ' 返回如下json字符串
        ' [
        '     {"PersonID":1,"Name":"Bryon Hetrick","Registered":true},
        '     {"PersonID":2,"Name":"Nicole Wilcox","Registered":true},
        '     {"PersonID":3,"Name":"Adrian Martinson","Registered":false},
        '     {"PersonID":4,"Name":"Nora Osborn","Registered":false}
        ' ]

        Dim deserializedResult = serializer.Deserialize(Of List(Of Person))(serializedResult)
        '返回peson类的list
    End Sub
End Class

未经允许不得转载:桔子雨工作室 » Vb.net序列化和反序列化json之JavaScriptSerializer对象
分享到: 生成海报
一个数字化服务提供商

承接外贸建站,软件APP开发

部分产品联系我们
切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

切换登录

注册

微信扫一扫关注
如已关注,请回复“登录”二字获取验证码