简体   繁体   中英

Geolocation classic asp json response

I'm trying to parse an json response from google maps geolocation api. I'm getting an json response with this code:

     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

I have tried to get the values for street adress (origin and destination and i have tried to get distance but i cant get it to work.

How do i get the values in the response so i can use it in my classic asp code?

I've tried different kind of solutions and this is based on an JSON class ( 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()

When running this i get LineNumber 55 ErrorCode 800a01c3 Description Object not a collection

Line 55 is (For Each subItem In oJSON.data("distance"))

The json response in my example is based on example coordinates and is'nt the actual location. The adresses is only containing standard az.

The load JSON does not work that way. You should load the response of your call to googleapis in there, not the request URL. So something like this:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM