【问题标题】:JSON VBA Parse to ExcelJSON VBA 解析到 Excel
【发布时间】:2018-10-08 16:29:30
【问题描述】:

我得到了一些 JSON 解析工作。我使用 VBA 解析来自我的网络服务器的 JSON 代码,将其写入我的 Excel 工作表的单元格 A1。但我无法将其转换为其他单元格。

这是我的 JSON 示例:

{
    "@type":["IN.areaList.1","OII.list.1"],
    "@self":"/bereiche",
    "list":[
          {"@type":["IN.bereich.1"],
           "@self":"/1.1.Bereich.2.7",
           "scha":false,
           "trlState":"",
           "oiischa":false,
           "readyTo1":false,
           "readyTo2":false,
           "numberOfBypassedDevices":0,
           "test":"",
           "TestActive":false,
           "chModeActive":false,
           "incs":[]}
            ]
}

这是我的 Sub,它正在为另一个示例工作:

Sub JsonToExcelExample()

    Dim jsonText As String
    Dim jsonObject As Object
    Dim item As Object
    Dim i As Long
    Dim ws As Worksheet

    Set ws = Worksheets("Remote")
    jsonText = ws.Cells(1, 1)
    Set jsonObject = JsonConverter.ParseJson(jsonText)
    i = 3
    ws.Cells(2, 1) = "Color"
    ws.Cells(2, 2) = "Hex Code"
    For Each item In jsonObject("0")
        ws.Cells(i, 1) = item("color")
        ws.Cells(i, 2) = item("value")
        i = i + 1
    Next

End Sub

应如何更改此 VBA 代码,以便将上述 JSON 示例像表格一样放置在工作表上?

【问题讨论】:

  • 要获得正确的代码格式,您必须在要显示为代码的每一行前面有 4 个空格。
  • 你能举个例子吗。首先我想从 TestActive 获得输出
  • 您可以申请JSON.ToArray并将结果数组输出到工作表,看看code examples。显示您拥有的样本的预期输出。
  • 您的 JSON 和 VBA 代码似乎不相关?你期待什么输出?

标签: json vba excel web-scraping


【解决方案1】:

看看下面的例子。 JSON.bas模块导入VBA项目进行JSON处理。

Option Explicit

Sub Test()

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

    ' Retrieve question #50068973 HTML content
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://stackoverflow.com/questions/50068973", False
        .send
        sJSONString = .responseText
    End With
    ' Extract JSON sample from the question
    sJSONString = "{" & Split(sJSONString, "<code>{", 2)(1)
    sJSONString = Split(sJSONString, "</code>", 2)(0)
    ' Parse JSON sample
    JSON.Parse sJSONString, vJSON, sState
    If sState = "Error" Then
        MsgBox "Invalid JSON"
        End
    End If
    ' Convert raw JSON to array and output to worksheet #1
    JSON.ToArray vJSON, aData, aHeader
    With Sheets(1)
        .Cells.Delete
        .Cells.WrapText = False
        OutputArray .Cells(1, 1), aHeader
        Output2DArray .Cells(2, 1), aData
        .Columns.AutoFit
    End With
    ' Flatten JSON
    JSON.Flatten vJSON, vResult
    ' Convert flattened JSON to array and output to worksheet #2
    JSON.ToArray vResult, aData, aHeader
    With Sheets(2)
        .Cells.Delete
        .Cells.WrapText = False
        OutputArray .Cells(1, 1), aHeader
        Output2DArray .Cells(2, 1), aData
        .Columns.AutoFit
    End With
    MsgBox "Completed"

End Sub

Sub OutputArray(oDstRng As Range, aCells As Variant)

    With oDstRng
        .Parent.Select
        With .Resize(1, UBound(aCells) - LBound(aCells) + 1)
            .NumberFormat = "@"
            .Value = aCells
        End With
    End With

End Sub

Sub Output2DArray(oDstRng As Range, aCells As Variant)

    With oDstRng
        .Parent.Select
        With .Resize( _
                UBound(aCells, 1) - LBound(aCells, 1) + 1, _
                UBound(aCells, 2) - LBound(aCells, 2) + 1)
            .NumberFormat = "@"
            .Value = aCells
        End With
    End With

End Sub

您提供的原始样本的工作表 #1 上的输出如下:

工作表 #2 上有扁平化的示例输出:

顺便说一句,类似的方法适用于in other answers

【讨论】:

  • 我发现这在大型数据集上非常慢。它看起来像:“Output2DArray .Cells(2, 1), aData”一直在占用。有什么建议?我试过 Application.ScreenUpdating = False 和 Application.Calculation = xlCalculationManual
  • @Marius AFAIK Output2DArray 使用 VBA 中最快的方法。
  • 编辑上面的评论:抱歉,实际上是 JSON.Parse 花了所有时间。有什么办法可以加快速度?
  • @Marius IMO 最好发布一个新问题,您可以在其中描述您遇到的性能问题。
猜你喜欢
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-12
  • 2018-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多