多语言展示
当前在线:1465今日阅读:6今日分享:31

VB.NET解析JSON

说在前面的话:本人刚开始自学VB.NET,之前学了一点点VB6,后来又接触了一下VB.NET感觉很多地方蛮好用的,就开始摸VB.NET。由于不是专业出身,所以很多地方的代码有着一股浓浓的山寨味,希望大家看到的时候多多谅解,谢谢。关于文中的解析方式,其实是根据JSON官方提供的VB6的那个例子来的,虽然官方没有为VB.NET专门再鼓捣一个DLL或者什么COM但是引用了文中的那两个东西以后,用法基本上是一致的,所以就有了下面的内容。文中所有知识均来自JSON网站提供的www点ediy点co点nz/vbjson-json-parser-library-in-vb6-xidc55680.html上的VB-JSON如有错误请多多指正,谢谢!
工具/原料
1

vb.net

2

Newtonsoft

3

json 串

方法/步骤
1

大家知道JSON是以“键:值”的形式来出现的,在vb.net中使用的时候,一般会需要将对应项目的值取出来,这时可以使用 json对象.item('KEY1').item('KEY2').ITEM('KEY3').tostring 这种形式来取KEY1下KEY2下KEY3的VALUE

2

如果遇到数组的话就在相应的item后面加一组括号,里面写上数组中第几个元素(不晓得叫啥好,就是数组中第几个用大括号括起来的东西) 对象.item('KEY1').item(0)('KEY2').ITEM('KEY3').tostring

3

某item下面有几个值对或数组可在item('KEY')后面加.count 对象.item('KEY1').item('KEY2').conut

4

某数组中添加值对(可能说的不准确,好像是只有被大括号包起来的VALUE里面可以增加值) 对象.Item('items').Item(0).Add('ExtraItem', 'Extra Data Value')

5

移除值对 对象.item('items').item(0).remove('url')

6

下面是自己写的一个小例子对上面的情况进行一个补充 'vb.net中的json解析其实和vb6中用官方的vbjson访问方式基本一致'下面是其基本操作的举例'需要引用这两个东西,不晓得是否都是必需的Imports Newtonsoft.JsonImports Newtonsoft.Json.Linq Public Class Form1    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click'Me.RichTextBox1.Text = '{ width: '200', frame: false, height: 130 ,bodyStyle:'background-color: #ffffcc;',buttonAlign:'right', items: [{ xtype: 'form', url: '/content.asp'},{ xtype: 'form2', url: '/content2.asp'}] }'        Dim s As String = Me.RichTextBox1.Text'把P定义为Object然后用ctype将JsonConvert.DeserializeObject(json字符串)弄出来的东西转换成JObject,就可以得到Json对象了Dim p As Object = CType(JsonConvert.DeserializeObject(s), JObject)        Dim str As String = '' '获取第一层共有几个值对        str = str & 'p.count:' & p.count & vbCrLf '获取第一层的items的KEY下面有几个值对        str = str & 'p.(''items'').count:' & p('items').count & vbCrLf '获取第一层的items的VALUE值        str = str & 'p(''items'').toString:' & p('items').ToString & vbCrLf '获取第一层的items下的数组的第一个用大括号括起来的那一串东西        str = str & 'p(''items'').item(0).ToString:' & p('items').item(0).ToString & vbCrLf '获取第一层的items下的数组的第一个用大括号括起来的那一串东西里面的名为url的KEY下面的值        str = str & 'p(''items'').item(0)(''url'').ToString:' & p('items').item(0)('url').ToString & vbCrLf '在第一层的items下的数组的第一个用大括号括起来的那一串东西里面添加一个值对        str = str & 'p.Item(''items'').Item(0).Add(''ExtraItem'',''Extra Data Value''):' & vbCrLfp.Item('items').Item(0).Add('ExtraItem', 'Extra Data Value') '这一句才是真正的添加新的值对,上面那句是用来在界面上显示相关信息的,没多大用处 '再次获取第一层的items下的数组的第一个用大括号括起来的那一串东西,看看添加上了新的值对没有str = str & 'p(''items'').item(0).ToString:' & p('items').item(0).ToString  & vbCrLf '移除获取第一层的items下的数组的第一个用大括号括起来的那一串东西里面的名为url的值对        str = str & 'p.Item(''items'').Item(0).remove(''url''):' & vbCrLf  p.item('items').item(0).remove('url')'这一句才是真正的移除值对,上面那句是用来在界面上显示相关信息的,没多大用处 '再次获取第一层的items下的数组的第一个用大括号括起来的那一串东西,看看移除的值对还在不在        str = str & 'p(''items'').item(0).ToString:' & p('items').item(0).ToString & vbCrLf        Me.RichTextBox2.Text = str    End SubEnd Class

7

'下面是上面的示例窗口的InitializeComponent事件代码,在使用JSON时不需要下面的代码 _Partial Class Form1    Inherits System.Windows.Forms.Form     'Form 重写 Dispose,以清理组件列表。    _    Protected Overrides Sub Dispose(ByVal disposing As Boolean)        Try            If disposing AndAlso components IsNot Nothing Then                components.Dispose()            End If        Finally            MyBase.Dispose(disposing)        End Try    End Sub     'Windows 窗体设计器所必需的    Private components As System.ComponentModel.IContainer     '注意: 以下过程是 Windows 窗体设计器所必需的    '可以使用 Windows 窗体设计器修改它。    '不要使用代码编辑器修改它。    _    Private Sub InitializeComponent()        Me.RichTextBox1 = New System.Windows.Forms.RichTextBox()        Me.RichTextBox2 = New System.Windows.Forms.RichTextBox()        Me.Button1 = New System.Windows.Forms.Button()        Me.SuspendLayout()        '        'RichTextBox1        '        Me.RichTextBox1.Location = New System.Drawing.Point(12, 12)        Me.RichTextBox1.Name = 'RichTextBox1'        Me.RichTextBox1.Size = New System.Drawing.Size(902, 164)        Me.RichTextBox1.TabIndex = 1        Me.RichTextBox1.Text = '{ width: '200', frame: false, height: 130, bodyStyle:'background-color: #ffffcc;',buttonAlign:'right', items: [{ xtype: 'form',  url: '/content.asp'},{ xtype: 'form2',  url: '/content2.asp'}] }'        '        'RichTextBox2        '        Me.RichTextBox2.Location = New System.Drawing.Point(12, 237)        Me.RichTextBox2.Name = 'RichTextBox2'        Me.RichTextBox2.Size = New System.Drawing.Size(902, 212)        Me.RichTextBox2.TabIndex = 2        Me.RichTextBox2.Text = ''        '        'Button1        '        Me.Button1.Location = New System.Drawing.Point(12, 182)        Me.Button1.Name = 'Button1'        Me.Button1.Size = New System.Drawing.Size(75, 23)        Me.Button1.TabIndex = 3        Me.Button1.Text = 'Button1'        Me.Button1.UseVisualStyleBackColor = True        '        'Form1        '        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font        Me.ClientSize = New System.Drawing.Size(926, 471)        Me.Controls.Add(Me.Button1)        Me.Controls.Add(Me.RichTextBox2)        Me.Controls.Add(Me.RichTextBox1)        Me.Name = 'Form1'        Me.Text = 'Form1'        Me.ResumeLayout(False)     End Sub    Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox    Friend WithEvents RichTextBox2 As System.Windows.Forms.RichTextBox    Friend WithEvents Button1 As System.Windows.Forms.Button End Class

注意事项

这里代码可能看起来挺费劲的,没办法,这里的编辑器不支持像论坛上那样可以将代码放到专门的框里面去,大家将就看吧

推荐信息