【问题标题】:Parse Json with Gson without POJO?用没有 POJO 的 Gson 解析 Json?
【发布时间】:2018-04-01 04:27:59
【问题描述】:

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

我正在尝试在此 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"
    }

以前使用 gson 时,我能够立即使用以下方法解析我的结果:

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 或响应类的情况下做到这一点?如果没有,有人可以解释如何/为什么这样做:

Parsing nested JSON data using GSON

【问题讨论】:

    标签: java json parsing gson pojo


    【解决方案1】:

    你快到了,你是正确的,你需要解析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());
    

    【讨论】:

    • 工作就像一个魅力!非常感谢。
    【解决方案2】:

    可以,但不一定非要使用 GSON。

    您可以使用 Jackson 将 JSON 解析为 Java 对象

    。比如你可以在没有POJO类的情况下获取:Object、List、Hashmap、Integer、String等。

    在android中首先添加以下行:

    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
    

    接下来导入映射器

    import com.fasterxml.jackson.databind.ObjectMapper
    

    最后你可以使用映射器传递字符串 JSON 值,例如:

    HashMap<*,*> object = new ObjectMapper().readValue(body, HashMap.class);
    

    希望对你有帮助。想了解更多关于杰克逊的信息,请访问:Jackson

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-02
      • 2011-04-26
      • 2021-06-02
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      相关资源
      最近更新 更多