2016-07-03 49 views
0

我正在使用VB-JSON解析器從API獲取數據並將數據保存在MS訪問表中。使用JSOn獲取MS Access中節點的值

我無法弄清楚如何訪問-KLj9kXnKd-9txfyIqM8-KLjJoT7gXCMq_jHx2_z

我有表結構如下,並希望保存數據如下所示。

|ServerID    |Name  |Mobile 
|-KLj9kXnKd-9txfyIqM8 |Adarsh  |9987 
|-KLjJoT7gXCMq_jHx2_z |Manas  |022 

JSON

{ 
    "-KLj9kXnKd-9txfyIqM8": { 
    "personmobile": "9987", 
    "personname": "Adarsh" 
    }, 
    "-KLjJoT7gXCMq_jHx2_z": { 
    "personmobile": "022", 
    "personname": "Manas" 
    } 
} 

VBA

Public Sub GetPerson() 
'I have code here which gets the json as above from api.  
Dim egTran As String  

If reader.Status = 200 Then 
    Set db = CurrentDb 
    Set rs = db.OpenRecordset("tblPerson", dbOpenDynaset, dbSeeChanges) 

    egTran = "[" & reader.responseText & "]" 
    Set coll = Json.parse(egTran) 

    For Each contact In coll 
     rs.AddNew 
     rs!Name = contact.Item("personname") 
     rs!Mobile = contact.Item("personmobile") 
     rs!ServerID = contact.Item("??????") 

我該怎麼寫??????

 rs.Update 
    Next 
End If 
End Sub 

我也打開使用任何其他解析器。該API是基於Firebase Database

回答

2

我不熟悉VB-JSON,但顯然「??????」不是聯繫人的項目。

因此,如果我運行這個測試功能:

Public Sub TestJsonText() 

    Dim DataCollection  As Collection 
    Dim ResponseText  As String 

    ResponseText = _ 
     "{" & _ 
     " ""-KLj9kXnKd-9txfyIqM8"": {" & _ 
     " ""personmobile"": ""9987""," & _ 
     " ""personname"": ""Adarsh""" & _ 
     " }," & _ 
     " ""-KLjJoT7gXCMq_jHx2_z"": {" & _ 
     " ""personmobile"": ""022""," & _ 
     " ""personname"": ""Manas""" & _ 
     " }" & _ 
     "}" 

    If ResponseText <> "" Then 
     Set DataCollection = CollectJson(ResponseText) 
     MsgBox "Retrieved" & Str(DataCollection.Count) & " root member(s)", vbInformation + vbOKOnly, "Web Service Success" 
    End If 

    Call ListFieldNames(DataCollection) 

    Set DataCollection = Nothing 

End Sub 

使用JSON模塊從VBA.CVRAPI它將打印:

root       
    -KLj9kXnKd-9txfy   
     personmobile  9987 
     personname   Adarsh 
    -KLjJoT7gXCMq_jH   
     personmobile  022 
     personname   Manas 

從功能ListFieldNames你可以挑選成員名稱字段名稱DataCollection(Index)(CollectionItem.Data)字段值添加記錄。