VB.Net 将 JSON 数组解析为 Key:Value 对的字典以获得与 Xojo 代码相同的结果,示例

问题描述

我需要在 VB.Net 中复制我在 Xojo 中可以做的事情。这将用于使用嵌入式 VB.Net 的基于柯达规则的自动化上的自定义代码。我如何让 VB.net 代码与我在下面展示的 Xojo 代码做同样的事情。尽管我已经搜索了很多解决方案,但我在 VB 中尝试的任何事情都没有做任何事情,只是在尝试使用字典来检索值时出现错误。我已经在印能捷中安装了 Newtonsoft DLL,因此我发现的用于“反序列化”JSON 的各种方法不会给我编译错误,但超越它以复制我在 Xojo 中可以做的事情正在逃避我。

我需要解析来自 REST API 的 JSON 响应以确定名称是否具有特定状态。我在这里搜索了示例,并尝试了多次尝试来调整我为 VB.Net 找到的内容,但是我无法通过以下任何代码来工作:

Dim jsonstring As String = responseFromServer
Dim Worksteps As Dictionary(Of String,String) = JsonConvert.DeserializeObject(Of Dictionary(Of String,String))(jsonstring)

以下是从正在运行的 VB.Net 代码返回的 JSON 示例:

{
  "error": null,"worksteps": [
    {
      "id": "_210504_125916572_029875","name": "Sheet_1  4/4","job": {
        "id": "060671-21","name": "2021 Laramie High School"
      },"status": "COMPLETED","amountPlanned": 544,"wastePlanned": 60,"amountProduced": 169,"wasteProduced": 69,"deviceid": "114","types": [
        "ConventionalPrinting"
      ],"sequenceType": "SheetfedPrinting","start": "2021-05-05T09:46:06-05:00","end": "2021-05-05T09:48:38-05:00","startPlanned": "2021-05-05T07:52:22-05:00","endplanned": null,"setuptimePlanned": 0,"prodtimePlanned": 0,"actualTimes": [
        {
          "timeTypeGroupName": "Production time","timeTypeName": "Execution time","duration": 33
        },{
          "timeTypeGroupName": "Production time","timeTypeName": "Setup time","duration": 79
        },{
          "timeTypeGroupName": "Auxiliary time","timeTypeName": "Auxiliary time","duration": 40
        },{
          "timeTypeGroupName": "","timeTypeName": "","duration": 0
        }
      ]
    },{
      "id": "_210506_072306983_020035","name": "Sheet_2  4/4","status": "WAITING","amountPlanned": 0,"wastePlanned": 0,"amountProduced": 0,"wasteProduced": 0,"deviceid": "XL106_Pool","start": null,"end": null,"startPlanned": null,"actualTimes": null
    }
  ]
}

这里正在运行 Xojo 2021r1.1 代码,它可以完成我试图在 VB.Net 中完成的工作:

Public Function ParseWorkstepsJSON(jsontext As String,sheetname As String) As Boolean
  ' Parse the JSON and Put the resulting Worksteps Array into a new Dictionary
  Var jsonDict As Dictionary = ParseJSON(jsontext)
  Var JobWorksteps() As Object = jsonDict.Lookup("worksteps",New Dictionary())
  
  ' Loop through each Action Dictionary in the Actions Dictionary and Process the data within into an Array of values
  Worksteps.Resizeto(JobWorksteps.LastIndex,1)
  For idx As Integer = 0 to JobWorksteps.LastIndex
    Var wkstp As Dictionary = Dictionary(JobWorksteps(idx))
    ' Process the data in the Action in the order we want to store it in the array
    
    ' WorkStep Name
    If Not (wkstp.Value("name") = Nil) Then
      Worksteps(idx,0) = wkstp.Value("name")
    Else
      Worksteps(idx,0) = ""
    End If
    
    ' WorkStep Status
    If Not (wkstp.Value("status") = Nil) Then
      Worksteps(idx,1) = wkstp.Value("status")
    Else
      Worksteps(idx,1) = ""
    End If
  Next
  
  ' Determine whether "Sheet_1" staus = "COMPLETED"
  Var b As Boolean
  Var s As String
  For idx As Integer = 0 To Worksteps.LastIndex
    s = Worksteps(idx,0).NthField(" ",1)
    If s = sheetname Then
      If Worksteps(idx,1) = "COMPLETED" Then
        Return True
      End If
    End If
  Next
  Return False 
End Function

我不必像在 Xojo 中那样将 JSON 放入 VB.Net 中的字典中,这是我正在尝试的,如果有不同的方法来完成确定特定“名称”值是否在JSON 有一个对应的“状态”,值为“COMPLETED”

解决方法

对于加载 JSON,似乎可以按照以下方式进行操作:

Dim read = Newtonsoft.Json.Linq.JObject.Parse(json)

这将返回一个 JObject,它看起来具有与 Xojo 具有的等效的方法,允许您遍历内容并检查值。