【问题标题】:Parsing out a field in MS Access that is CSV (JSON) data解析 MS Access 中的 CSV (JSON) 数据字段
【发布时间】:2020-04-03 19:48:23
【问题描述】:

我有一个加载到 MS Access 中的 CSV 数据文件。这样做没问题。我已经全部加载了。

我的问题是文件中的一条记录包含一个包含 CSV 数据(JSON 数据)的字段,该字段是行项目发票数据 (ProductData),它可能包含多达 25 个行项目。

我要做的是将该字段解析为不同表中的多条记录,所有记录均由“500”之后的数字相关,即交易编号。

有人对如何在 Microsoft Access 中执行此操作有任何想法吗?

样本数据:

500,3,30600052763,30128903358,"{""type"": ""reportingData"", ""merStoreId"": ""8135001"", ""productData"": [{""productCode"": ""001"", ""totalAmount"": ""20.00"", ""quantity"": ""16.000"", ""unitPrice"": ""1.25"", ""tax1Amount"": ""6.22""}, {""productCode"": ""102"", ""totalAmount"": ""14.00"", ""quantity"": ""1.000"", ""unitPrice"": ""14.00""}, {""productCode"": ""963"", ""totalAmount"": ""1.00"", ""quantity"": ""1.000"", ""unitPrice"": ""1.00"", ""tax1Amount"": ""0.00""}]}"

我附上了一个示例,说明我希望根据上面的事务号 3 显示的表格:

Tbl500                                      
Transaction Number  Second Field    Third Field Type            MerStoreID  ProductCode TotalAmount Quantity    UnitPrice   Tax1Amount  Tax2Amount
3                   30600052763     0128903358  reportingData   8135001     001         20.00       16.000      1.25        6.22    
3                   30600052763     30128903358 reportingData   8135001     102         14.00       1.000       14.00       
3                   30600052763     30128903358 reportingData   8135001     963         1.00        1.000       1.00        0.00    

【问题讨论】:

    标签: sql json excel parsing ms-access


    【解决方案1】:

    下面是一些获取文本文件的代码,导入数据并稍微整理一下,然后将其拆分为可变长度的数组。然后循环这个数组,并将数据添加到表中,使用“productCode”的存在来检测它是否是新记录:

    Sub sGetJSONData()
        On Error GoTo E_Handle
        Dim db As DAO.Database
        Dim rsData As DAO.Recordset
        Dim strFile As String
        Dim intFile As Integer
        Dim strInput As String
        Dim astrData() As String
        Dim intLoop1 As Integer
        Set db = DBEngine(0)(0)
        Set rsData = db.OpenRecordset("SELECT * FROM tblJSON WHERE 1=2;")
        strFile = "C:\test\json.txt"
        intFile = FreeFile
        Open strFile For Input As intFile
        Do
            Erase astrData
            Line Input #intFile, strInput
            strInput = Replace(strInput, Chr(34) & Chr(34), "")
            strInput = Replace(strInput, "productData: [{", "")
            strInput = Replace(strInput, "{productCode", "productCode")
            strInput = Left(strInput, Len(strInput) - 4)
            strInput = Replace(strInput, "}", "")
            strInput = Replace(strInput, ", ", ",")
            astrData = Split(strInput, ",")
            With rsData
                .AddNew
                !TransactionNumber = astrData(1)
                !SecondField = astrData(2)
                !ThirdField = astrData(3)
                !Type = Mid(astrData(4), 9)
                !ProductCode = Mid(astrData(6), 13)
                !MerStoreID = Mid(astrData(5), 13)
                For intLoop1 = 7 To UBound(astrData)
                    If Left(astrData(intLoop1), 11) = "totalAmount" Then !TotalAmount = Mid(astrData(intLoop1), 14)
                    If Left(astrData(intLoop1), 8) = "quantity" Then !Quantity = Mid(astrData(intLoop1), 11)
                    If Left(astrData(intLoop1), 9) = "unitPrice" Then !UnitPrice = Mid(astrData(intLoop1), 12)
                    If Left(astrData(intLoop1), 10) = "tax1Amount" Then !Tax1Amount = Mid(astrData(intLoop1), 13)
                    If Left(astrData(intLoop1), 10) = "tax2Amount" Then !Tax2Amount = Mid(astrData(intLoop1), 13)
                    If Left(astrData(intLoop1), 11) = "productCode" Then
                        .Update
                        .AddNew
                        !TransactionNumber = astrData(1)
                        !SecondField = astrData(2)
                        !ThirdField = astrData(3)
                        !Type = Mid(astrData(4), 9)
                        !ProductCode = Mid(astrData(intLoop1), 13)
                        !MerStoreID = Mid(astrData(5), 13)
                    End If
                Next intLoop1
                .Update
            End With
        Loop Until EOF(intFile)
    sExit:
        On Error Resume Next
        rsData.Close
        Set rsData = Nothing
       Reset
        Exit Sub
    E_Handle:
        MsgBox Err.Description & vbCrLf & vbCrLf & "sGetJSONData", vbOKOnly + vbCritical, "Error: " & Err.Number
        Resume sExit
    End Sub
    

    问候,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-27
      • 2018-09-04
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      相关资源
      最近更新 更多