PDA

View Full Version : Parsing xmlhttp.responseText


BRIANBOY12
11-01-2010, 10:33 PM
I am trying to parse an xmlhttp.responseText which is returned in JSON format. I specifically want to get the "value" of the "distance:text" keys ... i.e. "13.7 mi, 0.2 mi, etc". I'm not very familiar with server-side scripting. Most of my googling seems to indicate this should be straight forward and easy with the returnedText being in JSON format, but I can't seem to find an example of this using server-side scripting to parse the responseText. The server script below works fine, but it doesn't accomplish what I really want. Below the server script is a snippet of the responseText.

<%
url = "http://maps.google.com/?output=html&saddr='san leandro,ca'&daddr='san mateo,ca'"
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "GET", url, false
xmlhttp.send ""
myString = "text"
if instr(xmlhttp.responseText,myString)>0 then
response.write "found it !!"
else
response.write xmlhttp.responseText
end if
set xmlhttp = nothing
%>

The following is a snippet of the responseText:

"distance": {
"value": 22021,
"text": "13.7 mi"
}
}, {
"travel_mode": "DRIVING",
"start_location": {
"lat": 37.5517600,
"lng": -122.3098200
},
"end_location": {
"lat": 37.5510600,
"lng": -122.3127400
},
"distance": {
"value": 284,
"text": "0.2 mi"
}
}, {
"travel_mode": "DRIVING",
"start_location": {
"lat": 37.5510600,
"lng": -122.3127400
},
"end_location": {
"lat": 37.5628300,
"lng": -122.3257500
},

Old Pedant
11-02-2010, 12:50 AM
You do *NOT* want HTML output when doing this with VBScript. If you wanted to use JScript, you might use HTML. But for VBS, you'd be better off asking for XML.

VBS simply has no reasonable way to parse JSON. If you coded the page in JS, though, you could use eval() same as you would in a browser.

But if XML output is available, use it for VBS.

BRIANBOY12
11-10-2010, 02:38 AM
Thanks for your reply. You were correct, I tried coding in JS and was able to parse the response effectively. I am now stumbling on another problem which seems simple, but I can't find an answer.

<script language=javascript runat=server>
var pickup = "concord,ca";
var drop = "sunnyvale,ca";
var url = "http://maps.googleapis.com/maps/api/directions/json?origin=concord,ca&destination=drop&sensor=false";
var xmlhttp = new ActiveXObject("MSXML2.ServerXMLHTTP");
xmlhttp.open("GET", url, 0);
xmlhttp.send("");
var responseData = eval('(' + xmlhttp.responseText + ')');
// remainder of script cut
</script>

How do I reference the variable 'drop' in the url. The value is not being read as a parameter. I've tried different escape characters, but none work. If I insert the value instead of the variable it works fine.

Old Pedant
11-10-2010, 03:01 AM
WHAT???

There's no "drop" in the responseText!

Well, there is, but only because Google thought "drop" was the NAME of your destination city and comes back with
"start_address": "Concord, CA, USA",
"end_address": "Drop, TX 76247, USA",

I *seriously* doubt that was what you intended!

Is *THIS* what you are after????
var pickup = "concord,ca";
var drop = "sunnyvale,ca";
var url = "http://maps.googleapis.com/maps/api/directions/json?"
+ "origin=" + escape(pickup) + "&destination=" + escape(drop) + "&sensor=false";
...

???

Nothing to do with "parsing".

Okay...re-reading your question, I guess this is exactly what you are after.

No?

BRIANBOY12
11-10-2010, 06:52 AM
Thanks again. That is exactly what I was looking to do. I did realize Google was interpreting the "drop" as literal, but, I couldn't escape the variable "drop". I appreciate you taking the time to help me. I am used to coding Cold Fusion, but sometimes I get involved in coding JS and VB .. just not very well.