您的位置:首页 > Web前端 > JavaScript

VB.NET 操作Json 包括嵌套的

2012-06-14 01:12 429 查看
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


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: