0

我正在尝试解析来自 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 代码中使用它?

4

2 回答 2

0

加载 JSON 不是这样工作的。您应该在其中加载对 googleapis 的调用响应,而不是请求 URL。所以是这样的:

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)
于 2013-11-07T07:04:53.983 回答
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 Description Object not a collection

第 55 行是 (For Each subItem In oJSON.data("distance"))

我的示例中的 json 响应基于示例坐标,而不是实际位置。地址仅包含标准 az。

于 2013-10-17T19:16:20.927 回答