json - 使用 VBA 将 JSON 解析为 Excel

标签 json excel vba

我在解析 VBA 中的 JSON 数据时遇到了一些麻烦。我已经尝试了所有在线示例,但仍然无法解决问题。我设法做的是使用从另一个网站提取数据的另一个 VBA 代码以原始格式将 JSON 数据提取到 excel 中。我已经粘贴了下面的代码。它不是很干净,并且有一些重复,因为我只是想看看我是否可以提取数据。
我尝试使用 VBA 解析数据的所有尝试都失败了,并出现了各种错误,具体取决于我采用的方法。如果有人能给我一些关于解析我设法提取的数据的最简单方法的建议,我将不胜感激。我需要的只是列中的数据,然后我可以在工作簿的其他工作表中使用这些数据。我附上了一张我提取的数据的图片。我设法解析了来自另一个网页的 JSON 数据,并且在代码中我包含了 JSON 数据的每个列标题。对于这个新网页,JSON 数据是嵌套的,并且有很多独特的行,所以我没有采用这种方法。非常感谢

[Sub JSONPull()
Dim WB As Workbook, ws As Worksheet, ws2 As Worksheet, qtb As QueryTable
Dim FC As String, sDate As String, eDate As String, Dockmasterurl As String, Performance As Worksheet

Set WB = Application.ThisWorkbook
Set ws = WB.Sheets("Control")

FC = ws.Range("B5")
sDate = ws.Range("B14")
eDate = ws.Range("B15")
Dim sJSONString As String
    Dim vJSON
    Dim sState As String
    Dim aData()
    Dim aHeader()
    Dim vResult

Dockmasterurl = "https://fc-inbound-dock-execution-service-eu-eug1-dub.dub.proxy.amazon.com/appointment/bySearchParams?warehouseId=" & FC & "&clientId=dockmaster&localStartDate=" & sDate & "T00%3A00%3A00&localEndDate=" & eDate & "T08%3A00%3A00&isStartInRange=false&searchResultLevel=FULL"

Set ws2 = Sheets("JSON")
ws2.Cells.ClearContents

Set qtb = ws2.QueryTables.Add("URL;" & Dockmasterurl, ws2.Range("A1"))
With qtb
    .WebSelectionType = xlEntirePage
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = True
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With

ws2.Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, textqualifier:=xlDoubleQuote, consecutivedelimiter:=False, comma:=True, trailingminusnumbers:=True
ws2.Range("A:S").EntireColumn.AutoFit

For Each qtb In ws2.QueryTables
    qtb.Delete

Next

End Sub][1]

最佳答案

这是 VBA 示例,显示 JSON sample by the link可以转换为二维数组并输出到工作表。 进口 JSON.bas模块到 VBA 项目中进行 JSON 处理。

Option Explicit

Sub Test()

    Dim sJSONString As String
    Dim vJSON
    Dim sState As String
    Dim aData()
    Dim aHeader()

    ' Retrieve JSON content
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://pastebin.com/raw/hA2UEDXy", True
        .send
        Do Until .readyState = 4: DoEvents: Loop
        sJSONString = .responseText
    End With
    ' Parse JSON sample
    JSON.Parse sJSONString, vJSON, sState
    If sState = "Error" Then MsgBox "Invalid JSON": End
    ' Convert JSON to 2D Array
    JSON.ToArray vJSON("AppointmentList"), aData, aHeader
    ' Output to worksheet #1
    Output aHeader, aData, ThisWorkbook.Sheets(1)
    MsgBox "Completed"

End Sub

Sub Output(aHeader, aData, oDestWorksheet As Worksheet)

    With oDestWorksheet
        .Activate
        .Cells.Delete
        With .Cells(1, 1)
            .Resize(1, UBound(aHeader) - LBound(aHeader) + 1).Value = aHeader
            .Offset(1, 0).Resize( _
                    UBound(aData, 1) - LBound(aData, 1) + 1, _
                    UBound(aData, 2) - LBound(aData, 2) + 1 _
                ).Value = aData
        End With
        .Columns.AutoFit
    End With

End Sub

我的输出如下(点击放大):

output

顺便说一句,应用了类似的方法 in other answers .

关于json - 使用 VBA 将 JSON 解析为 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57649256/

相关文章:

java - 读取 GZIP 文件导致 Java 中 ZLIB 输入流意外结束

javascript - 如何轻松测量 JSON 对象的复杂性?

arrays - 数组未在 For 循环中返回答案

vb.net - 避免 "Microsoft Excel"提示

php - 高效存储数据

android - GSON 自定义解串器处理 Null

java - Apache POI 无法检测哈希格式的数字

vba - 将pdf放在新创建的同名文件夹中

excel - 在 VBA 中使用 Substring 和 IndexOf 方法

excel - 当单元格中的新数据发生更改时,将一行数据移到顶部