您可以使用 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