我正在尝试使用VB.NET读取一个简单的JSON文件,但我真的找不到答案,因为无论尝试如何,都会出现错误。我正在使用Newtonsoft.Json。我不确定是否需要使用Dim jsonArray As JArray = JArray.Parse(json)或作为对象将其解析为json数组。我想循环对象并将每个元素写入变量,以便可以将它们保存到数据库中。我已经在javascript中完成了此操作,但是vb却把我扔了。
[
{
"Organization":"a",
"Ref_ID":"33",
"First":"Bob",
"MI":"V",
"Last":"Smith",
"Suffix":""
},
{
"Organization":"a",
"Ref_ID":"12",
"First":"Mary",
"MI":"",
"Last":"Jones",
"Suffix":""
},
{
"Organization":"Stony Brook",
"Ref_ID":"74",
"First":"Jonas",
"MI":"S",
"Last":"Green",
"Suffix":""
}
]
答案 0 :(得分:1)
您可以使用Newtonsoft轻松地将JSON转换为(类)对象列表。下面的示例使用带有单个文本框和按钮的表单。
Imports Newtonsoft.Json
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Const JSON As String = "[^ {^ 'Organization':'a',^ 'Ref_ID':'33',^ 'First':'Bob',^ 'MI':'V',^ 'Last':'Smith',^ 'Suffix':''^ },^ {^ 'Organization':'a',^ 'Ref_ID':'12',^ 'First':'Mary',^ 'MI':'',^ 'Last':'Jones',^ 'Suffix':''^ },^ {^ 'Organization':'Stony Brook',^ 'Ref_ID':'74',^ 'First':'Jonas',^ 'MI':'S',^ 'Last':'Green',^ 'Suffix':''^ }^]"
txtJson.Text = JSON.Replace("^", vbCrLf).Replace("'", """")
End Sub
Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click
Try
Dim xReturn As List(Of RandomPerson) = JsonConvert.DeserializeObject(Of List(Of RandomPerson))(txtJson.Text)
Dim sMessage As String = ""
For Each OnePerson As RandomPerson In xReturn
sMessage &= OnePerson.Ref_ID & " // " & OnePerson.First & " // " & OnePerson.Last & vbCrLf
Next OnePerson
MessageBox.Show(Me, sMessage, "Done", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch Exp As Exception
MessageBox.Show(Me, Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
End Class
'The class below was created by converting the example JSON into a C# class using: http://json2csharp.com/
'You can then convert the C# class into a VB.NET class by hand, or by using a tool like: http://www.carlosag.net/Tools/CodeTranslator/
Public Class RandomPerson
Public Property Organization As String = ""
Public Property Ref_ID As String = ""
Public Property First As String = ""
Public Property MI As String = ""
Public Property Last As String = ""
Public Property Suffix As String = ""
End Class
答案 1 :(得分:0)
假设js变量包含您的json字符串,则如下所示:
Dim reader As New JsonTextReader(New StringReader(js))
While reader.Read
If reader.Value IsNot Nothing Then
If reader.Path.Contains("Ref_ID") Then
Console.WriteLine("Ref_ID::" + reader.Value)
End If
End If
End While
请记住,读取器会遍历每个键和值,例如在第一个读取读取器中。路径将是“ [0] Organization”,而reader.Value将是“ Organization”。第二读reader.Path将是“ [0] Organization”,而reader.Value将是它的值“ a”。 [0]表示记录编号,当reader.Path =“ [1] .Ref_ID”时,您的值将为12
答案 2 :(得分:0)
或者,您也可以像这样:
Imports System.Web.Script.Serialization ' for reading of JSON (+add the reference to System.Web.Extensions library)
Dim JSONItems = New JavaScriptSerializer().DeserializeObject(JSOn_string)
类似于我在另一个问题中回答的内容 Comparing files not working as intended
答案 3 :(得分:-1)
尝试类似的操作
Dim json As String = rawresp
Dim jsonObject As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(json)
Dim jsonArray As JArray = jsonObject("result")
For Each item As JObject In jsonArray
textboxLast.Text = item.SelectToken("Last").ToString
Next