自助推荐
立即入驻

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

博客2年前更新 桔子雨
1,142 00
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

© 版权声明

相关文章