我公司有一家供应商提供JSON数据源,我需要每两小时将其加载到MS Access数据库中。我需要:
我遇到了this question讨论类似问题,但是在MS Access中如何实现这一点并没有很好的描述。感谢任何帮助!
答案 0 :(得分:7)
使用VBA JSON库,您当然可以将JSON格式的文件导入MS Access。我们的想法是将JSON数据视为字典集合,Visual Basic提供collection和dictionary作为数据结构。
以下是步骤:
<强> JSON 强>
[
{
"col1": somenumber,
"col2": "somestring",
"col3": "somestring",
"col4": "somestring",
"col5": "somestring"
}
]
VBA代码
Private Function JSONImport()
Dim db As Database, qdef As Querydef
Dim FileNum As Integer
Dim DataLine As String, jsonStr As String, strSQL As String
Dim p As Object, element As Variant
Set db = CurrentDb
' READ FROM EXTERNAL FILE
FileNum = FreeFile()
Open "C:\Path\To\JsonFile.json" For Input As #FileNum
' PARSE FILE STRING
jsonStr = ""
While Not EOF(FileNum)
Line Input #FileNum, DataLine
jsonStr = jsonStr & DataLine & vbNewLine
Wend
Close #FileNum
Set p = ParseJson(jsonStr)
' ITERATE THROUGH DATA ROWS, APPENDING TO TABLE
For Each element In p
strSQL = "PARAMETERS [col1] Long, [col2] Text(255), [col3] Text(255), " _
& "[col4] Text(255), [col5] Text(255); " _
& "INSERT INTO TableName (col1, col2, col3, col4, col5) " _
& "VALUES([col1], [col2], [col3], [col4], [col5]);"
Set qdef = db.CreateQueryDef("", strSQL)
qdef!col1 = element("col1")
qdef!col2 = element("col2")
qdef!col3 = element("col3")
qdef!col4 = element("col4")
qdef!col5 = element("col5")
qdef.Execute
Next element
Set element = Nothing
Set p = Nothing
End Function
答案 1 :(得分:-3)
MS Access中的Json文件处理非常简单。只需将.json扩展名重命名为.txt,并使用 text 导入功能,并将分隔符设置为(:),文本分隔符设置为(“)。 一行代码......快乐的编码!