Excel VBA extract values JSON URL -


in excel

i try extract value "45.33887499999999"

by google url "https://maps.googleapis.com/maps/api/geocode/json?address=bojon" (in example google url +"=bojon" or +"=venice%20beach%20ca")

with vba code:

public function latitude(coord string)     dim firstval string     firstval = "https://maps.googleapis.com/maps/api/geocode/json?address="     set objhttp = createobject("msxml2.serverxmlhttp")     url = firstval & replace(coord, " ", "+")     objhttp.open "get", url, false     objhttp.setrequestheader "user-agent", "mozilla/4.0 (compatible; msie 6.0; windows nt 5.0)"     objhttp.send ("")     if instr(objhttp.responsetext, """location"" : {") = 0 goto errorhandl     set regex = createobject("vbscript.regexp"): regex.pattern = """lat"".*?([0-9]+)": regex.global = false     set matches = regex.execute(objhttp.responsetext)     tmpval = replace(matches(index).submatches(0), ".", application.international(xllistseparator))     latitude = cdbl(tmpval)     exit function errorhandl:     latitude = -1 end function 

but code extracts "45" , not "45.33887499999999"

i tried change regex.pattern = """lat"".*?([0-9]+)"

but haven't found solution


finally want extract 3 values 3 different formulas (created vba code) url

google url + "=bojon"

in these rows

     "formatted_address" : "30010 bojon ve, italia",      "geometry" : {         "location" : {            "lat" : 45.33887499999999,            "lng" : 12.06598 

in a1 cell: "bojon"

=googdress(a1) result = "30010 bojon ve, italia"

=latitude(a1) result = "45.33887499999999"

=longitude(a1) result = "12.06598"


another example:

google url + "=venice%20beach%20ca"

     "formatted_address" : "venice beach, california, stati uniti",      "geometry" : {         "bounds" : {            "northeast" : {               "lat" : 33.996311,               "lng" : -118.4561299            },            "southwest" : {               "lat" : 33.9636437,               "lng" : -118.4835886            }         },         "location" : {            "lat" : 33.9936153,            "lng" : -118.4799099 

=googdress(a1) result = "venice beach, california, stati uniti"

=latitude(a1) result = "33.9936153"

=longitude(a1) result = "-118.4799099"


can me?

using script control parse json , caching json responses avoid unnecessary xmlhttp calls:

sub tester()      debug.print getresult("https://maps.googleapis.com/maps/api/geocode/json?address=bojon", _                        "results[0].geometry.location.lat")      debug.print getresult("https://maps.googleapis.com/maps/api/geocode/json?address=bojon", _                        "results[0].geometry.location.lng")   end sub  function getresult(url string, jsonpath string)      static responsecache object     dim objhttp object, json string     dim sc object      if responsecache nothing         set responsecache = createobject("scripting.dictionary")     end if      if not responsecache.exists(url)         debug.print "fetching:" & url         set objhttp = createobject("msxml2.serverxmlhttp")         objhttp.open "get", "https://maps.googleapis.com/maps/api/geocode/json?address=bojon", false         objhttp.send ("")         json = objhttp.responsetext         responsecache.add url, json     else         debug.print "use cache:" & url         json = responsecache(url)     end if      set sc = createobject("scriptcontrol")     sc.language = "jscript"     sc.eval "var obj=(" & json & ")" 'evaluate json response      getresult = sc.eval("obj." & jsonpath)  end function 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -