0

I need to develop a program where I have to calculate driving distance and time. Getting the request to the Google Maps API works, and I get a really big response. Now I want to extract the data I need from this response. The response is in json, and the program I make is made in Java. I have no idea to do this. I tried to just put the whole response in a string, and then search the data I need, like "distance" and extract a few characters that come after that. Problem with this is, if something changes in front of it, it's all going to be at different indexes in the huge string.

A response is like this (only a bit of the big thing)

"copyrights" : "Kaartgegevens ©2017 GeoBasis-DE/BKG (©2009), Google",
     "legs" : [
        {
           "distance" : {
              "text" : "229 km",
              "value" : 229411
           },
           "duration" : {
              "text" : "2 uur 20 min.",
              "value" : 8417
           },

How do I easily extract the value of both "distance" and "duration"?

1 Answers1

0

Basically what you want to do is serializing and deserializing the JSON to a proper Java object. You can do this by using Frameworks like Jackson or GSON to convert the JSON-String to POJOs.

This might help you as well:

How to serialize and deserialize a JSON object from Google geocode using Java

Converting JSON to Java

http://thegeekyland.blogspot.de/2015/11/serializing-and-deserializing-json-from.html

And I'm pretty sure Google has it's own API that maps the JSON internally, although I haven't used it yet. Here you might find more information:

https://developers.google.com/maps/web-services/?hl=de

Hope this helps!

Community
  • 1
  • 1