【发布时间】:2016-09-01 12:23:55
【问题描述】:
我不断收到“类型不匹配错误”(表明它不是数组?):
Sub FillTaxiInfo
For i = 0 To UBound(data("prices")) - 1
代码正在尝试解析来自的 JSON(参见下面的“价格”):
{"id":1,"prices":[{"name":"expressTaxi","fare":{"fareType":"standard", "base":"$2.50"...}}
当我放置断点并检查“价格”时,它告诉我“值”是未在上下文中定义的表达式,“类型”为空。
任何其他改进建议将不胜感激。
我的完整代码:
Option Explicit
Sub Run()
Dim myUrls As Variant
myUrls = Array("URL1, URL2, URL3")
FillMultipleCityInfo myUrls, ActiveWorkbook
End Sub
Function GetJson(ByVal url As String) As Dictionary
With New WinHttpRequest
.Open "GET", url
.Send
Set GetJson = JsonConverter.ParseJson(.ResponseText)
End With
End Function
Sub FillTaxiInfo(data As Dictionary, sheet As Worksheet)
Dim i As Integer, taxi As Dictionary
For i = 0 To UBound(data("prices")) - 1
Set taxi = data("prices")(i)
If taxi.Exists("name") Then
sheet.Cells(i, 1) = taxi("name")
sheet.Cells(i, 2) = taxi("fare")("fareType")
End If
Next i
End Sub
Sub FillMultipleCityInfo(urls As Variant, book As Workbook)
Dim i As Integer, data As Dictionary, sheet As Worksheet
For i = 0 To UBound(urls) - 1
Set data = GetJson(urls(i))
Set sheet = book.Sheets(i + 1)
FillTaxiInfo data, sheet
Next i
End Sub
【问题讨论】:
标签: json vba type-mismatch