2015-04-03 61 views
-2

我試圖從外部源讀取JSON字符串。這裏是字符串 -從嵌套的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": "" 
    } 
} 

我能夠編碼和檢索的錯誤,這是該代碼與「數據」部分設置爲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」向前訪問數據?

下面是一個使用字典

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

我然後解析將JSON的該部分到一個新的對象我對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

聞起來像VB.Net,而不是VB6。 – Bob77 2015-04-04 01:11:06

回答

0

類創建用於timing_matched_pa​​ir類並用字典鍵加載列表

 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)

然後能夠負荷的按鍵成一個列表