2

i would like to ask for your help in this matter. I have never worked with Json / Gson before, here is what i want to do.

  1. For a studyproject we want to use the HERE API that provides a String that contains the time of a route between two adresses. Generating the URL that contains the two adresses latitude and longitude works pretty good already. If i click on the link its opening this page:

JSON VIEW:

1

RAW String:

{
  "response": {
    "metaInfo": {
      "timestamp": "2020-06-09T06:25:38Z",
      "mapVersion": "8.30.109.150",
      "moduleVersion": "7.2.202021-7166",
      "interfaceVersion": "2.6.76",
      "availableMapVersion": [
        "8.30.109.150"
      ]
    },
    "route": [
      {
        "waypoint": [
          {
            "linkId": "+1131186975",
            "mappedPosition": {
              "latitude": 51.3547214,
              "longitude": 6.1533969
            },
            "originalPosition": {
              "latitude": 51.3547199,
              "longitude": 6.1534
            },
            "type": "stopOver",
            "spot": 0.1907895,
            "sideOfStreet": "neither",
            "mappedRoadName": "Tegelseweg",
            "label": "Tegelseweg - N271",
            "shapeIndex": 0,
            "source": "user"
          },
          {
            "linkId": "+1151950438",
            "mappedPosition": {
              "latitude": 52.3654734,
              "longitude": 4.8532104
            },
            "originalPosition": {
              "latitude": 52.3654734,
              "longitude": 4.8532099
            },
            "type": "stopOver",
            "spot": 0.3333333,
            "sideOfStreet": "neither",
            "mappedRoadName": "Hoofdweg",
            "label": "Hoofdweg",
            "shapeIndex": 1367,
            "source": "user"
          }
        ],
        "mode": {
          "type": "fastest",
          "transportModes": [
            "truck"
          ],
          "trafficMode": "disabled",
          "feature": []
        },
        "leg": [
          {
            "start": {
              "linkId": "+1131186975",
              "mappedPosition": {
                "latitude": 51.3547214,
                "longitude": 6.1533969
              },
              "originalPosition": {
                "latitude": 51.3547199,
                "longitude": 6.1534
              },
              "type": "stopOver",
              "spot": 0.1907895,
              "sideOfStreet": "neither",
              "mappedRoadName": "Tegelseweg",
              "label": "Tegelseweg - N271",
              "shapeIndex": 0,
              "source": "user"
            },
            "end": {
              "linkId": "+1151950438",
              "mappedPosition": {
                "latitude": 52.3654734,
                "longitude": 4.8532104
              },
              "originalPosition": {
                "latitude": 52.3654734,
                "longitude": 4.8532099
              },
              "type": "stopOver",
              "spot": 0.3333333,
              "sideOfStreet": "neither",
              "mappedRoadName": "Hoofdweg",
              "label": "Hoofdweg",
              "shapeIndex": 1367,
              "source": "user"
            },
            "length": 183631,
            "travelTime": 8364,
            "maneuver": [
              {
                "position": {
                  "latitude": 51.3547214,
                  "longitude": 6.1533969
                },
                "instruction": "Head toward <span class=\"toward_street\">Hulsterweg</span> on <span class=\"street\">Tegelseweg</span> <span class=\"number\">(N271)</span>. <span class=\"distance-description\">Go for <span class=\"length\">123 m</span>.</span>",
                "travelTime": 16,
                "length": 123,
                "id": "M1",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 51.3555801,
                  "longitude": 6.154511
                },
                "instruction": "Make a U-Turn onto <span class=\"next-street\">Tegelseweg</span> <span class=\"number\">(N271)</span>. <span class=\"distance-description\">Go for <span class=\"length\">274 m</span>.</span>",
                "travelTime": 126,
                "length": 274,
                "id": "M2",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 51.3541317,
                  "longitude": 6.1523652
                },
                "instruction": "Turn <span class=\"direction\">right</span> and take ramp onto <span class=\"number\">A73</span> toward <span class=\"sign\"><span lang=\"nl\">Eindhoven</span>/<span lang=\"nl\">Nijmegen</span></span>. <span class=\"distance-description\">Go for <span class=\"length\">64.3 km</span>.</span>",
                "travelTime": 2746,
                "length": 64315,
                "id": "M3",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 51.8447292,
                  "longitude": 5.7708693
                },
                "instruction": "Keep <span class=\"direction\">right</span> onto <span class=\"number\">A73</span>. <span class=\"distance-description\">Go for <span class=\"length\">3.8 km</span>.</span>",
                "travelTime": 166,
                "length": 3793,
                "id": "M4",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 51.856252,
                  "longitude": 5.7191348
                },
                "instruction": "Take the exit toward <span class=\"sign\"><span lang=\"nl\">Rotterdam</span>/<span lang=\"nl\">Arnhem</span></span> onto <span class=\"number\">A50</span>. <span class=\"distance-description\">Go for <span class=\"length\">2.3 km</span>.</span>",
                "travelTime": 102,
                "length": 2338,
                "id": "M5",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 51.8729675,
                  "longitude": 5.7295954
                },
                "instruction": "Keep <span class=\"direction\">left</span> onto <span class=\"number\">A50</span>. <span class=\"distance-description\">Go for <span class=\"length\">2.5 km</span>.</span>",
                "travelTime": 103,
                "length": 2471,
                "id": "M6",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 51.893481,
                  "longitude": 5.7424915
                },
                "instruction": "Keep <span class=\"direction\">left</span> onto <span class=\"number\">A50</span>. <span class=\"distance-description\">Go for <span class=\"length\">1.4 km</span>.</span>",
                "travelTime": 61,
                "length": 1440,
                "id": "M7",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 51.9014311,
                  "longitude": 5.7587779
                },
                "instruction": "Take the exit toward <span class=\"sign\"><span lang=\"nl\">Rotterdam</span>/<span lang=\"nl\">Tiel</span></span> onto <span class=\"number\">A15</span>. <span class=\"distance-description\">Go for <span class=\"length\">39.8 km</span>.</span>",
                "travelTime": 1735,
                "length": 39789,
                "id": "M8",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 51.8583441,
                  "longitude": 5.2429676
                },
                "instruction": "Take the exit toward <span class=\"sign\"><span lang=\"nl\">Utrecht</span></span> onto <span class=\"number\">A2</span>. <span class=\"distance-description\">Go for <span class=\"length\">59.0 km</span>.</span>",
                "travelTime": 2555,
                "length": 58990,
                "id": "M9",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 52.3197055,
                  "longitude": 4.9161673
                },
                "instruction": "Keep <span class=\"direction\">right</span> onto <span class=\"number\">E35</span> <span class=\"next-street\">(Knooppunt Amstel)</span> toward <span class=\"sign\"><span lang=\"nl\">E19</span>/<span lang=\"nl\">RING</span>/<span lang=\"nl\">Luchthaven Schiphol</span>/<span lang=\"nl\">Zaanstad</span>/<span lang=\"nl\">A10</span></span>. <span class=\"distance-description\">Go for <span class=\"length\">714 m</span>.</span>",
                "travelTime": 32,
                "length": 714,
                "id": "M10",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 52.3259926,
                  "longitude": 4.9142146
                },
                "instruction": "Keep <span class=\"direction\">left</span> onto <span class=\"next-street\">Knooppunt Amstel</span> toward <span class=\"sign\"><span lang=\"nl\">Luchthaven Schiphol</span>/<span lang=\"nl\">A10</span>/<span lang=\"nl\">E19</span></span>. <span class=\"distance-description\">Go for <span class=\"length\">1.1 km</span>.</span>",
                "travelTime": 70,
                "length": 1140,
                "id": "M11",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 52.3305416,
                  "longitude": 4.9042583
                },
                "instruction": "Keep <span class=\"direction\">right</span> onto <span class=\"number\">A10</span> <span class=\"next-street\">(Ring Amsterdam)</span>. <span class=\"distance-description\">Go for <span class=\"length\">6.3 km</span>.</span>",
                "travelTime": 321,
                "length": 6343,
                "id": "M12",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 52.3544562,
                  "longitude": 4.8427069
                },
                "instruction": "Take exit <span class=\"exit\">6</span> toward <span class=\"sign\"><span lang=\"nl\">Osdorp</span>/<span lang=\"nl\">Overtoomse Veld</span>/<span lang=\"nl\">Oud-West</span></span>. <span class=\"distance-description\">Go for <span class=\"length\">367 m</span>.</span>",
                "travelTime": 63,
                "length": 367,
                "id": "M13",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 52.3577499,
                  "longitude": 4.8430288
                },
                "instruction": "Turn <span class=\"direction\">right</span> onto <span class=\"next-street\">Cornelis Lelylaan</span> <span class=\"number\">(s106)</span> toward <span class=\"sign\"><span lang=\"nl\">Oud-West</span>/<span lang=\"nl\">Centrum</span></span>. <span class=\"distance-description\">Go for <span class=\"length\">555 m</span>.</span>",
                "travelTime": 77,
                "length": 555,
                "id": "M14",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 52.3579645,
                  "longitude": 4.8511183
                },
                "instruction": "Keep <span class=\"direction\">left</span> toward <span class=\"sign\"><span lang=\"nl\">De Baarsjes</span></span>. <span class=\"distance-description\">Go for <span class=\"length\">126 m</span>.</span>",
                "travelTime": 31,
                "length": 126,
                "id": "M15",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 52.358104,
                  "longitude": 4.8529422
                },
                "instruction": "Take the <span class=\"exit\">2nd exit</span> from Surinameplein roundabout onto <span class=\"next-street\">Surinameplein</span>. <span class=\"distance-description\">Go for <span class=\"length\">134 m</span>.</span>",
                "travelTime": 39,
                "length": 134,
                "id": "M16",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 52.3590052,
                  "longitude": 4.8534894
                },
                "instruction": "Continue on <span class=\"next-street\">Hoofdweg</span>. <span class=\"distance-description\">Go for <span class=\"length\">719 m</span>.</span>",
                "travelTime": 121,
                "length": 719,
                "id": "M17",
                "_type": "PrivateTransportManeuverType"
              },
              {
                "position": {
                  "latitude": 52.3654734,
                  "longitude": 4.8532104
                },
                "instruction": "Arrive at <span class=\"street\">Hoofdweg</span>.",
                "travelTime": 0,
                "length": 0,
                "id": "M18",
                "_type": "PrivateTransportManeuverType"
              }
            ]
          }
        ],
        "summary": {
          "distance": 183631,
          "trafficTime": 8427,
          "baseTime": 8364,
          "flags": [
            "tollroad",
            "tunnel",
            "motorway",
            "builtUpArea"
          ],
          "text": "The trip takes <span class=\"length\">184 km</span> and <span class=\"time\">2:19 h</span>.",
          "travelTime": 8364,
          "_type": "RouteSummaryType"
        }
      }
    ],
    "language": "en-us"
  }
}

I only need to have the yellow marked part of the whole Json String (traffic time: 8364). What is the easiest way to get this? My current code looks like this, it returns null:

 URL host = new URL(uri);
    HttpURLConnection con = (HttpURLConnection) host.openConnection();
    con.setRequestMethod("GET");


    System.out.println(
        "Response code - message: " + con.getResponseCode() + " - " + con.getResponseMessage());

    if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
        Reader reader = new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8);
        Gson gson = new GsonBuilder().create();

The con.getResponseMessage prints Connection okay, so the link works.

  1. I googled several solutions but i cannot import the necessary libraries, IntelliJ displays an error if i try to import them for applying this solution i found on stackoverflow: How to get the JSON code of a web page

Import of Json Library:

Im new to import external libraries thats why i dont know how to handle this

Thank you for your help! :)

2
  • Please edit and update the question with json as text instead of image. Commented Jun 9, 2020 at 7:10
  • Checkout Rest-Assured Commented Jun 9, 2020 at 7:13

2 Answers 2

2

You can create model classes and use GSON to parse them. This will make it easy for you to access any field from json response.

  1. Model classes
class ResponseData {
    private Response response;
    // getters and setters
}

class Response {
    private List<Route> route;
    // getters and setters
}

class Route {
    private Summary summary;
    // getters and setters
}

class Summary {
    private Long trafficTime;
    // getters and setters
}
  1. Parse json response to models
Reader reader = new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8);
Gson gson = new Gson();
ResponseData responseData = gson.fromJson(reader, ResponseData.class);
responseData.getResponse().getRoute().forEach(route -> System.out.println(route.getSummary().getTrafficTime()));

Output:

8427

Sign up to request clarification or add additional context in comments.

1 Comment

you made my day. Thank you very much.
2

If you are using Gson probaly, keep going this way and don't need to import any Json lib.

JsonParser parser = new JsonParser();
JsonElement json = parser.parse(your_json_string);
JsonArray route = json.getAsJsonObject().get("response").getAsJsonObject().get("route")getAsJsonArray();
int yourTrafficTime = route.get(0).getAsJsonObject().get("summary").getAsJsonObject().get("trafficTime").getAsInt();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.