• VB.NET 操作Json 包括嵌套的


    Imports System.Runtime.Serialization.Json
    Imports System.IO
    
    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim strTest As String
            'strTest = "{""name"":""Peggy"",""email"":""peggy@gmail.com""}"
    
            'Button1.Attributes.Add("onclick", "json(" & strTest & ")")
    
    
    
            strTest = "{""result"":[{""content"":""你好"",""sendPhone"":""15523565211"",""replayTime"":""2011-09-27 09:45:15.887""},{""content"":""大家好"",""sendPhone"":""15523565211"",""replayTime"":""2011-09-27 09:42:48.933""},{""content"":""都好"",""sendPhone"":""15523565211"",""replayTime"":""2011-09-27 09:34:31.727""}],""success"":true}"
    
    
    
            'Dim mytest As Test
            'mytest = FromJosnT(Of Test)(strTest)
            'Response.Write(mytest.name & " " & mytest.email)
    
            Dim myTest As content
            myTest = FromJosnT(Of content)(strTest)
            For Each ab As BackContent In myTest.result
                Response.Write(ab.content & ab.sendPhone & ab.replayTime)
            Next
            Response.Write(myTest.success)
        End Sub
    
        Public Function FromJosn(ByVal josn As String) As Object
            Dim ds As DataContractJsonSerializer = New DataContractJsonSerializer(GetType(Test))
            Dim ms As MemoryStream = New MemoryStream(System.Text.Encoding.UTF8.GetBytes(josn))
            Return ds.ReadObject(ms)
        End Function
    
        Public Function ToJosnT(Of T)(ByVal myT As T) As String
            Dim ds = New DataContractJsonSerializer(GetType(T))
            Dim ms = New MemoryStream()
            ds.WriteObject(ms, myT)
    
            Dim strReturn As String
            strReturn = Encoding.UTF8.GetString(ms.ToArray())
            ms.Close()
            Return strReturn
        End Function
    
        Public Function FromJosnT(Of T)(ByVal josn As String) As T
            Dim ds As DataContractJsonSerializer = New DataContractJsonSerializer(GetType(T))
            Dim ms As MemoryStream = New MemoryStream(System.Text.Encoding.UTF8.GetBytes(josn))
            Return ds.ReadObject(ms)
        End Function
    
        Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim myTest As New Test()
            myTest.name = "test"
            myTest.email = "adfas@163.com"
    
            Response.Write(ToJosnT(Of Test)(myTest))
        End Sub
    End Class
    
    
    Public Class Test
        Private _name As String
        Public Property name As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property
    
        Private _email As String
        Public Property email As String
            Get
                Return _email
            End Get
            Set(ByVal value As String)
                _email = value
            End Set
        End Property
    End Class
    
    Public Class BackContent
        Private _content As String
        Public Property content As String
            Get
                Return _content
            End Get
            Set(ByVal value As String)
                _content = value
            End Set
        End Property
    
    
        Private _sendPhone As String
        Public Property sendPhone As String 
            Get
                Return _sendPhone
            End Get
            Set(ByVal value As String)
                _sendPhone = value
            End Set
        End Property
    
    
        Private _replayTime As String
        Public Property replayTime As String 
            Get
                Return _replayTime
            End Get
            Set(ByVal value As String)
                _replayTime = value
            End Set
        End Property
    End Class
    
    Public Class content
        Private _result As IList(Of BackContent)
        Public Property result As IList(Of BackContent)
            Get
                Return _result
            End Get
            Set(ByVal value As IList(Of BackContent))
                _result = value
            End Set
        End Property
    
        Private _success As String
        Public Property success As String 
            Get
                Return _success
            End Get
            Set(ByVal value As String)
                _success = value
            End Set
        End Property
    End Class
    

      

  • 相关阅读:
    看一看Vue.js
    Docker笔记
    Termux使用记录
    【Selenium】利用Cookies登录京东并添加商品至购物车以及结算
    GB 26149-2017 测试项目及要求
    【LeetCode】41. 缺失的第一个正数
    【数据结构】7. 排序
    【数据结构】6. 查找
    【数据结构】5. 图
    【数据结构】4. 树与二叉树
  • 原文地址:https://www.cnblogs.com/allan5204/p/2548709.html
Copyright © 2020-2023  润新知