我正在尝试解析来自google maps geolocation api的json响应。我用这段代码得到了一个json响应:
set xmlHTTP = server.createobject("MSXML2.ServerXMLHTTP.6.0")
xmlHTTP.open "GET", "https://maps.googleapis.com/maps/api/distancematrix/json?origins=50.9255685,29.2703104&destinations=46.446251,28.570993&mode=driving&language=sv-SE&sensor=false", false
xmlHTTP.send()
LatLongFeed = xmlHTTP.ResponseText
我试图获取街道地址的值(来源和目的地,我试图找到距离,但我无法让它工作。
如何获取响应中的值,以便在经典的asp代码中使用它?
答案 0 :(得分:0)
我尝试过不同类型的解决方案,这是基于JSON类(http://www.aspjson.com/)
Set oJSON = New aspJSON
'Load JSON string
oJSON.loadJSON("https://maps.googleapis.com/maps/api/distancematrix/json?origins=" & rs("LatiStart")& "," & rs("LongStart")& "&destinations=" & rs("LatiStop")& "," & rs("LongStop")& "&mode=driving&language=sv-SE&sensor=false")
'Get single value
Response.Write oJSON.data("distance") & "<br>"
'Loop through collection
For Each subItem In oJSON.data("distance")
Response.Write subItem & ": " & _
oJSON.data("distance").item(subItem) & "<br>"
Next
'Return the object
Response.Write oJSON.JSONoutput()
跑步时我得到了 LineNumber 55 ErrorCode 800a01c3 说明对象不是集合
第55行是(对于每个子项目在oJSON.data(“距离”))
我的示例中的json响应基于示例坐标,并且不是实际位置。地址仅包含标准的a-z。
答案 1 :(得分:0)
加载JSON不起作用。您应该在那里加载对googleapis的通话响应,而不是请求网址。 所以像这样:
URL = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" & rs("LatiStart")& "," & rs("LongStart")& "&destinations=" & rs("LatiStop")& "," & rs("LongStop")& "&mode=driving&language=sv-SE&sensor=false"
Set objXmlHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
objXmlHTTP.Open "POST", URL, False
objXmlHTTP.setRequestHeader "Content-Type", "application/json"
objXmlHttp.Send
Set oJSON = New aspJSON
oJSON.loadJSON(objXmlHTTP.ResponseText)