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
相关文章推荐
- vb.net 教程 4-11 Json文件操作 1
- vb.net 教程 4-11 Json文件操作 2 JavaScriptSerializer
- vb.net 教程 4-11 Json文件操作 3 DataContractJsonSerializer
- VB.NET用Newtonsoft.Json操作json字符串
- vb.net 教程 8-3 数据库操作9-1
- ASP.NET FTP的操作(包括上传、下载、重命名、删除、检查是否存在)
- TTF1控件的基本操作VB.NET代码
- DataGridView在vb.net中的操作技巧
- [VB.NET]简单的 XML 文档操作
- VB.NET利用Windows API 实现关机、注销、重启等操作
- Java中Json字符串直接转换为对象(包括多层List集合 嵌套)
- C#(VB.NET)操作Windows自带的防火墙 之 启用(开启)/禁用(关闭)防火墙
- VB.Net操作Excel
- vb.net操作注册表(增加 修改 删除 查询)
- VB.NET操作WORD(VBA)
- vb.net 教程 4-2 目录操作 DirectoryInfo 1
- VB.NET操作SQL Server完全模块
- VB.NET中字符串操作函数
- vb.net 教程 4-4 目录操作 Directory 2
- VB.NET 操作 ACCESS OLE 字段内容