【发布时间】:2019-06-06 21:52:58
【问题描述】:
我构建了一个 VBA 代码,以便通过输入我之前使用我的手机使用条形扫描仪应用扫描的图书的 ISBN 代码从 Google Books API 获取数据。 使用VBA-JSON 库,一切似乎都还可以,但我仍然有一个无法导入的对象。
我用来检查代码是否有效的 JSON 文件是这样的:
https://www.googleapis.com/books/v1/volumes?q=isbn:9780553897852
这是我现在用来挑选数据的代码:
Public Sub exceljson()
'Error message if active cell is empty
If ActiveCell.Value = 0 Then
MsgBox "Select cell with ISBN", vbExclamation
Exit Sub
End If
'Error message if there is no match
On Error GoTo ErrMsg
Dim http As Object, JSON As Object, i As Integer, subitem As Object
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", "https://www.googleapis.com/books/v1/volumes?q=isbn:" & ActiveCell.Value, False
http.send
Set JSON = ParseJson(http.responseText)
i = ActiveCell.Row
For Each Item In JSON("items")
Set subitem = Item("volumeInfo")
Sheets(1).Cells(i, ActiveCell.Column + 1).Value = subitem("publishedDate")
Sheets(1).Cells(i, ActiveCell.Column + 2).Value = subitem("title")
Sheets(1).Cells(i, ActiveCell.Column + 3).Value = subitem("subtitle")
Sheets(1).Cells(i, ActiveCell.Column + 4).Value = subitem("pageCount")
'To obtain ISBN-10 and ISBN-13
j = 5
For Each Child In subitem("industryIdentifiers")
Sheets(1).Cells(i, ActiveCell.Column + j).Value = Child("identifier")
j = j + 1
Next
i = i + 1
'To end with success
Next
MsgBox ("Process complete"), vbInformation
Exit Sub
'To en with an error message
ErrMsg:
MsgBox ("No match obtained"), vbCritical
End Sub
This is the resulted EXCEL sheet I made
实际上,我在 ActiveCell 的后续单元格中显示了以下字段:出版年份、标题、副标题、页数、ISBN-10、ISBN-13 我写的是 ISBN。 但是我不知道如何从“作者”数组中收集数据。 是我唯一想念的数据字段,所以如果你能帮助我,我真的很感激。 提前致谢。
【问题讨论】:
-
For Each a In Item("VolumeInfo")("authors") -
不,它不起作用。由于没有大括号并且内部没有相等的事实,所以库似乎不解析数组。无论如何,谢谢你的想法。
标签: json excel vba api web-scraping