大概代码,有些行删掉了。
'调用接口 Set http = CreateObject("MSXML2.ServerXMLHTTP") URL = "http://xxxx.com/query" http.Open "POST", URL, False http.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" http.send request If http.Status = 200 Then 'MsgBox http.responseText
'解析JSON Set jsObj = CreateObject("msscriptcontrol.scriptcontrol") jsObj.Language = "JavaScript" jsObj.addcode ("var result = " & http.responseText) jsObj.addcode "var routeCount = result.routings.length" jsObj.addcode "var routings = result.routings"
'行程对象 Set routings = CallByName(jsObj.CodeObject, "routings", VbGet) '行程数量 routeCount = CallByName(routings, "length", VbGet)
If routeCount = 0 Then Range("A5").Value = "请求数据失败。" Exit Sub End If '遍历行程 excelRowNumber = 5 For rowNumber = 1 To routeCount '表格边框 Range("A" & excelRowNumber & ":L" & excelRowNumber).Borders.LineStyle = 1 '数据 Set Row = CallByName(routings, rowNumber - 1, VbGet) '行对象 '行程 routecodes = CallByName(Row, "routecodes", VbGet) '价格数组 Set priceInfo = CallByName(Row, "priceInfo", VbGet) priceCount = CallByName(priceInfo, "length", VbGet) For priceNumber = 1 To priceCount Set priceRow = CallByName(priceInfo, priceNumber - 1, VbGet) '行对象 '乘客类型 passengerTypeName = CallByName(priceRow, "passengerTypeName", VbGet)
Next
Cells(excelRowNumber, 1).Value = "" Cells(excelRowNumber, 2).Value = "" Cells(excelRowNumber, 3).Value = ""
excelRowNumber = excelRowNumber + 1 ContinueLoop: Next '排序 Range("A5:L" & (excelRowNumber - 1)).Sort key1:=Range("I5"), order1:=xlAscending, Header:=xlNo Else Range("A5").Value = "请求数据失败。" End If
CallByName可以看这个,类似于java里的反射。 https://www.cnblogs.com/zzstone/p/6274637.html?utm_source=itdadao&utm_medium=referral
|