在没有POJO的情况下用Gson解析Json? [英] Parse Json with Gson without POJO?

查看:113
本文介绍了在没有POJO的情况下用Gson解析Json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我正在尝试在此json响应中解析"formatted_address"的字符串:

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"
    }

以前使用gson时,我可以使用以下命令立即解析结果:

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());

主要区别在于我无法将此结果放入JsonArray主体中.相反(我相信,因为它具有嵌套的数据),我必须使其成为JsonObject,但是如果没有得到Null,就无法在我的生命中对其进行解析.有没有做POJO或响应类的简单方法吗?如果不能,那么有人可以这样解释为什么/为什么这样做:

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:

使用GSON解析嵌套的JSON数据

推荐答案

您快到了,并且您正确地需要解析JsonObject.

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());

这篇关于在没有POJO的情况下用Gson解析Json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆