Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Json with Gson without POJO?

Hoping there is an easy solution from someone on here. I know there are similar questions but I can't seem to modify them to work with my problem.

I am trying to parse the string for "formatted_address" in this json response:

    {
    "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Google Building 42",
               "short_name" : "Google Bldg 42",
               "types" : [ "premise" ]
            },
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Google Bldg 42, 1600 Amphitheatre Pkwy, Mountain 
    View, CA 94043, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 37.42198310000001,
                  "lng" : -122.0853195
               },
               "southwest" : {
                  "lat" : 37.4214139,
                  "lng" : -122.0860042
               }
            },
            "location" : {
               "lat" : 37.4216548,
               "lng" : -122.0856374
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4230474802915,
                  "lng" : -122.0843128697085
               },
               "southwest" : {
                  "lat" : 37.4203495197085,
                  "lng" : -122.0870108302915
               }
            }
         },
         "place_id" : "ChIJPzxqWQK6j4AR3OFRJ6LMaKo",
         "types" : [ "premise" ]
      }
   ],
   "status" : "OK"
    }

When previously using gson I was able to parse my result right away using:

Gson gson = new Gson();

    JsonArray body = gson.fromJson(ResultString, JsonArray.class);
    System.out.println(body.get(0).getAsJsonObject().get("elementnamehere").getAsString());

The main difference is that I can't put this result into JsonArray body. Instead (I believe since it has nested data) I have to make it a JsonObject, but I can not parse it for the life of me without getting Null. Any easy way to do this without making a POJO or Response Classes? If not can someone explain how/why to do that like so:

Parsing nested JSON data using GSON

like image 271
Tevor Avatar asked Oct 20 '17 02:10

Tevor


1 Answers

You are almost there and you are correct that you need to parse JsonObject.

JsonObject body = gson.fromJson(json, JsonObject.class);
JsonArray results = body.get("results").getAsJsonArray();
JsonObject firstResult = results.get(0).getAsJsonObject();
JsonElement address = firstResult.get("formatted_address");
System.out.println(address.getAsString());
like image 157
zafrost Avatar answered Oct 04 '22 00:10

zafrost