Attempting to get the value of an element or itself inside any element in Json returns null

Hi, I am trying to get the value of an element from JsonObject. I am using java with the Gson Library.

here is the related piece of code:

String s = "http://maps.googleapis.com/maps/api/directions/json?origin=30.065634,31.209473&destination=29.984177,31.440052&sensor=false";
URL url = new URL(s);

BufferedReader r = new BufferedReader(new InputStreamReader(
    ((HttpURLConnection) url.openConnection()).getInputStream()));

JsonStreamParser jsp = new JsonStreamParser(r);

JsonParser jp = new JsonParser();

JsonElement jsonElement =  jp.parse(r);

JsonObject jsonObject = jsonElement.getAsJsonObject();

System.out.println(jsonObject.get("summary"));

here is the json part:

"status": "OK",   "routes": [ {
    "summary": "Mehwar Al Moneeb",
    "legs": [ {
      "steps": [ {
        "travel_mode": "DRIVING",
        "start_location": {
          "lat": 30.0655100,
          "lng": 31.2096600
        },
        "end_location": {
          "lat": 30.0654400,
          "lng": 31.2096000
        },
        "polyline": {
          "points": "mdovDksn}DLJ",
          "levels": "BB"
        },
        "duration": {
          "value": 1,
          "text": "1 min"
        },
        "html_instructions": "Head \u003cb\u003esouthwest\u003c/b\u003e on \u003cb\u003eOmar Toson\u003c/b\u003e toward \u003cb\u003eMohammed Roshdi\u003c/b\u003e",
        "distance": {
          "value": 9,
          "text": "9 m"
        }
      }

When I get the top element "routes", its thin and printed, it displays what is inside the element, but when I try, for example, to get the "summary" of the element as shown, the return is null, this is what is displayed in the output. therefore, subsequently I cannot get its value. What's wrong? How to get the value of any JsonElement? Please answer me as soon as you can. Thanks in advance.

+3
source share
1

- javascript. , jsonObject.getAsJsonArray("routes").get(0).get("summary"); . Java-, JS jsonObject.routes[0].summary

: GSON .

0

Source: https://habr.com/ru/post/1779909/


All Articles