与没有POJO的Gson一起解析Json?

Tev*_*vor 7 java parsing json pojo gson

希望这里的人有一个简单的解决方案.我知道有类似的问题,但我似乎无法修改它们来解决我的问题.

我试图在这个json响应中解析"formatted_address"的字符串:

    {
    "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"
    }
Run Code Online (Sandbox Code Playgroud)

以前使用gson我可以使用以下方法立即解析我的结果:

Gson gson = new Gson();

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

主要区别在于我无法将此结果放入JsonArray体中.相反(我相信,因为它有嵌套数据)我必须使它成为一个JsonObject,但我无法解析它为我的生活而没有得到Null.没有制作POJO或响应类的任何简单方法吗?如果没有,有人可以解释如何/为什么这样做:

使用GSON解析嵌套的JSON数据

小智 12

你几乎就在那里,你是正确的,你需要解析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());
Run Code Online (Sandbox Code Playgroud)