【问题标题】:Read a JSON file vb.net Newtonsoft读取 JSON 文件 vb.net Newtonsoft
【发布时间】:2018-07-06 17:28:12
【问题描述】:

我正在尝试使用 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":""
   }
]

【问题讨论】:

    标签: vb.net json.net


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      假设 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
      

      请记住 reader 会遍历每个键和值,例如在第一次读取 reader.Path 将是“[0]Organization”和 reader.Value 将是“Organization”。第二次读取 reader.Path 将是“[0]Organization”,而 reader.Value 将是它的值“a”。 [0] 表示记录号,当 reader.Path="[1].Ref_ID" 时,您的值为 12

      【讨论】:

        【解决方案3】:

        或者你可以这样做:

        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

        【讨论】:

          【解决方案4】:

          试试这样的

          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”的属性。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-10-02
          • 1970-01-01
          • 2022-12-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多