VB.NET解析JSON

 时间:2024-10-16 02:51:29

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.LinqPublic 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""):" & vbCrLfp.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时不需要下面的代码<G造婷用痃lobal.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _Partial Class Form1 Inherits System.Windows.Forms.Form 'Form 重写 Dispose,以清理组件列表。 <System.Diagnostics.DebuggerNonUserCode()> _ 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 窗体设计器修改它。 '不要使用代码编辑器修改它。 <System.Diagnostics.DebuggerStepThrough()> _ 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.ButtonEnd Class

  • 管理360安全卫士的核晶防护如何操作
  • Windows Server 2012如何自定义管理控制台?
  • win10怎么限制应用上传下载速度 如何禁止联网
  • 怎样来解决dns异常?
  • win 10更新后,有线网卡不能使用,时断时续
  • 热门搜索
    长城手抄报图片 国学经典手抄报花边 儿童手抄报春节 我中国梦手抄报内容 海量阅读手抄报 少先队员手抄报内容 三年级上英语手抄报 十二生肖手抄报 我爱我班手抄报内容 读书手抄报版面设计