【发布时间】:2019-02-08 20:13:11
【问题描述】:
我正在尝试使用 VBA 将写入 sheet1 单元格 (B11:B15) 的 API 中的 JSON 数据解析为 excel: 单元格 B11 中的 API =
API 相同,只改变 ID
这是我正在使用的代码:
Option Explicit
Public r As Long, c As Long
Sub readValues()
Dim sJSONString As String
Dim ws As Worksheet
Dim a As Integer
Dim ID As String
Dim I As Integer
For a = 11 To 15
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", Foglio1.Cells(a, 2), False
.send
sJSONString = .responseText
'MsgBox sJSONString
End With
Dim JSON As Object, item As Object
ID = Foglio1.Cells(a, 1)
Set JSON = JsonConverter.ParseJson(sJSONString)("data")(ID)("statistics")("all")
r = 1: c = 1
EmptyDict JSON
Next a
End Sub
Public Sub EmptyDict(ByVal dict As Object)
Dim key As Variant, item As Object
Select Case TypeName(dict)
Case "Collection"
For Each item In dict
c = c
r = r + 1
EmptyDict item
Next
Case "Dictionary"
For Each key In dict
If TypeName(dict(key)) = "Collection" Then
EmptyDict (dict(key))
Else
With ThisWorkbook.Worksheets("foglio1")
.Cells(r + 9, c + 5) = (key)
.Cells(r + 10, c + 5) = dict(key)
End With
c = c + 1
End If
Next
End Select
End Sub
代码运行良好,但无法循环 5 个 ID API;该代码将所有 5 个项目写入同一行 11. 此外,我想在每一行中写入“所有”、“评级”对象以及“昵称”和“最后战斗时间”。 有人可以帮助我吗? 谢谢
【问题讨论】:
标签: json excel vba web-scraping