【问题标题】:Nested JSON VBA (records and fields)嵌套 JSON VBA(记录和字段)
【发布时间】:2021-07-03 01:02:08
【问题描述】:

我有这个 JSON 文件,但我不知道它是嵌套的还是什么,如果是,我应该使用集合吗?我的目标是使用 VBA 将其解析为 Excel 工作表。到目前为止,我只能解析名称。这种类型的数据有什么有用的链接吗?我只找到了普通的 JSON,但没有这样的。我是 JSON 新手,但在实习期间必须这样做。

示例 JSON:

{"records":[{"id":"rec1B74TQVtWwU6cW","fields":{"Name":"compriband","Materiaux":["recPqxRrg3tFC5o6T"]},"createdTime":"2021-03-07T18:22:47.000Z"},{"id":"rec3ZdAlSQhXCVG4c","fields":{"Name":"velux","Materiaux":["recig1Rh8WFpqe0wD","recAha2bQ5BTNED9V","recWAj3FZRsPj65Gz","recfv8V2t0Pje2Llg"]},"createdTime":"2021-03-07T18:27:27.000Z"}

我的代码尝试:

Private Sub CommandButton1_Click()

Dim W As Worksheet
Set W = ActiveSheet

'Read column names from row 1. Should match Airtable column names. No empty columns.
Dim fields As String
colCount = 2
Do Until IsEmpty(W.Cells(2, colCount))
fields = fields & "&fields[]=" & W.Cells(2, colCount).Value
colCount = colCount + 1
Loop

'Get the data from airtable

Dim http As New WinHttpRequest
Dim resp As String
Dim url As String

url = "https://api.airtable.com/v0/appY6Wo3AmLHqHkjr/categorie?api_key=key_here" & fields
http.Open "GET", url, False
http.Send
Debug.Print "Resultats " + CStr(http.ResponseText)
Dim json As Object
Set json = JsonConverter.ParseJson(http.ResponseText)

respRecord = 1
On Error GoTo Exit_Loop

Do Until json("records")(respRecord)("fields")(W.Cells(1, 1).Value) = ""

For respCol = 1 To colCount - 1
    cellValue = json("records")(respRecord)("fields")(W.Cells(1, respCol).Value)
    W.Cells(respRecord + 1, respCol).Value = cellValue
Next
respRecord = respRecord + 1
Loop

Exit_Loop:

End Sub

【问题讨论】:

  • 欢迎来到 SO!如果您可以发布您迄今为止尝试过的代码并告诉我们您遇到的问题,那将非常有用。
  • 嘿,我添加了到目前为止所做的事情
  • 不清楚你为什么认为这个 JSON 有问题,或者你期望“正常”JSON 是什么样子。当然是嵌套的;这几乎是大多数 JSON 的一个识别特征。
  • @triplee 不必刻薄,我说我是一个初学者,困扰我的是记录 [“id”,...] 我应该声明记录是一个集合还是一个数组?我看不出记录是什么类型。
  • @ChaymaB 到目前为止你还没有做坏事。但是,您发布的 JSON 无效,您在字符串的最后缺少 ]}。我想这在复制粘贴过程中丢失了,您发送的请求正在返回正确的字符串,对吧?

标签: arrays json excel vba api


【解决方案1】:

正如我在 cmets 中所说,到目前为止,您还没有做坏事。但是,您对 JSON 的结构缺乏一定程度的了解。

this 之类的工具可能会帮助您理解结构。

另外,我看到您使用了 VBA JSON parser 。文档中的示例应该可以帮助您更好地理解如何解析 JSON 字符串。

话虽如此,我会指出一些对你有帮助的东西:

您的 JSON 如下所示:

基本上,您有一个名为records 的数组,由[] 指定,由两个项目01 组成。这 2 项是嵌套的 JSON。所以你的初始 JSON 是一个 JSON 数组。

要显式访问数组的各个项目,您可以这样做:

json("records")(1)
json("records")(2)
...
json("records")(n)

现在,每个项目都由自己的元素组成。这些元素可以是数组、嵌套的 json 等。您的项目包含两个参数 idcreatedTime 以及一个嵌套的 JSON fields

要进一步了解每个项目,您可以使用以下语法:

json("records")(1)("nameOfParameter")

json("records")(1)("nameOfNestedJSON")

所以如果你想要第一个项目id 你会这样做:

Debug.Print json("records")(1)("id")

要遍历数组的所有项目,您可以这样做:

Dim item As Object
For Each item In json("records")
    Debug.Print item("id")
Next item

嵌套的 JSON fields 由两个参数 NamecreatedTime 以及一个数组 Materiaux 组成。

你可以像这样获取参数:

Debug.Print json("records")(1)("fields")("Name")

当然你也可以这样做:

Dim item As Object
For Each item In json("records")
    Debug.Print item("fields")("Name")
Next item

您可以更深入地访问Materiaux 数组的元素:

Debug.Print json("records")(2)("fields")("Materiaux")(1)

甚至循环遍历它们:

Dim arrayItem As Variant
For Each arrayItem In json("records")(2)("fields")("Materiaux")
    Debug.Print arrayItem
Next arrayItem

【讨论】:

    猜你喜欢
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多