从嵌套的JSON数组中检索数据

时间:2015-04-03 22:56:01

标签: json vb6

我正在尝试从外部源读取JSON字符串。这是字符串 -

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "success": true,
    "data": {
      "params": {
        "timing": {
          "timing_matched_pairs": {
            "11": {
              "store_id": "12345",
              "date": "2015-03-15",
              "menuboard_time": "2015-03-15 16:54:08",
              "menuboard_duration": 10,
              "pickup_time": "2015-03-15 16:54:27",
              "pickup_duration": 10,
              "total_duration": 29
            }
          }
        }
      }
    },
    "error_num": 0,
    "error_message": ""
  }
}

我能够为错误编写和检索响应字符串,这是将“data”部分设置为NULL的代码。我的具体问题似乎是时间_matched_pa​​irs数组。每行应该是一个ID和一个包含细节的对象。 (对“11”包含传奇的12345,日期等)

我正在使用此测试模拟从站点获取JSON -

Dim testJson As String = "{'jsonrpc':'2.0','id':1,'result':{'success':true,'data':{'params':{'timing':{'timing_matched_pairs':{'11':{'store_id':'12345','date':'2015-03-15','menuboard_time':'2015-03-1516:54:08','menuboard_duration':10,'pickup_time':'2015-03-15 16:54:27','pickup_duration':10,'total_duration':29}}}}},'error_num':0,'error_message':''}}"
Dim myObj As New dtd_Msg
myObj = JsonConvert.DeserializeObject(Of dtd_Msg)(testJson)

我可以使用以下内容查看一些数据 -

appMsgBox.AppendText("JsonRPC: " & myObj.jsonrpc & vbCrLf)
appMsgBox.AppendText("ID:      " & myObj.id & vbCrLf)
appMsgBox.AppendText("Result.success:   " & myObj.result.success & vbCrLf)

问题:如何从“timing_matched_pa​​irs”前进访问数据?

以下是我的JSON数据类

Public Class dtd_Msg
    Public Property jsonrpc As String
    Public Property id As Integer
    Public Property result As dtd_Result

End Class

Public Class dtd_Result
    Public Property success As Boolean
    'Public Property data As Object
    Public Property data As dtd_Data
    Public Property error_num As Integer
    Public Property error_message As String
End Class

Public Class dtd_Data
    Public Property params As dtd_Timing
End Class

Public Class dtd_Timing
    Public Property timing_matched_pairs As dtd_MatchedPairs
End Class

Public Class dtd_MatchedPairs
    Public Property uid As Integer
    Public Property readings As List(Of dtd_Readings)
End Class

Public Class dtd_Readings
    Public Property store_id As String
    Public Property evtdate As String
    Public Property menuboard_time As String
    Public Property menuboard_duration As Integer
    Public Property pickup_time As String
    Public Property pickup_duration As Integer
    Public Property total_duration As Integer

End Class

1 个答案:

答案 0 :(得分:0)

使用Dictionary

为timing_matched_pa​​ir创建了类

    Public Class dtd_Timing
        Public Property timing_matched_pairs As Dictionary(Of String,Object)
    End Class

然后我将Json的那部分解析为一个新对象并加载一个带有字典键的列表

        myDictionary = myObj.result.data.params.timing.timing_matched_pairs

        Dim list As New List(Of String)(myDictionary.Keys)

从那里可以轻松地通过键进行迭代,解析相应的对象并获取数据。

   myJson = JObject.Parse(myDictionary.Item(str).ToString)
   appMsgBox.AppendText(myJson.SelectToken("store_id").ToString & vbCrLf)
   appMsgBox.AppendText(myJson.SelectToken("date").ToString & vbCrLf)

然后才能将密钥加载到列表中