读取JSON文件vb.net Newtonsoft
我正在尝试使用VB.NET读取一个简单的JSON文件,但我真的找不到答案,因为无论我尝试什么,都会得到一个错误。我正在使用Newtonsoft.Json。我不确定是否需要使用dimJSONArray作为JArray=JArray.parse(json)或作为对象将其解析为json数组。我想循环对象并将每个元素写入变量,以便将它们保存到数据库中。我已经在javascript中完成了这项工作,但是vb把我甩了读取JSON文件vb.net Newtonsoft,vb.net,json.net,Vb.net,Json.net,我正在尝试使用VB.NET读取一个简单的JSON文件,但我真的找不到答案,因为无论我尝试什么,都会得到一个错误。我正在使用Newtonsoft.Json。我不确定是否需要使用dimJSONArray作为JArray=JArray.parse(json)或作为对象将其解析为json数组。我想循环对象并将每个元素写入变量,以便将它们保存到数据库中。我已经在javascript中完成了这项工作,但是vb把我甩了 [ { "Organization":"a", "R
[
{
"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":""
}
]
假设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]组织”,读卡器。值将是“组织”。Secound read reader。路径应为“[0]组织”和读取器。值应为其值“a”。
[0]表示记录编号,当reader.Path=“[1].Ref_ID”时,您的值将为12
Imports System.Web.Script.Serialization ' for reading of JSON (+add the reference to System.Web.Extensions library)
Dim JSONItems = New JavaScriptSerializer().DeserializeObject(JSOn_string)
类似于我在另一个问题中的回答
您可以使用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
试试这样的方法
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
您的代码不会像编写的那样工作——JSON是一个数组,而不是一个对象,并且它在任何地方都不包含名为“result”的属性。