【发布时间】: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 无效,您在字符串的最后缺少
]}。我想这在复制粘贴过程中丢失了,您发送的请求正在返回正确的字符串,对吧?