【问题标题】:How to fix Gson JSON parsing from LinkedTreeMap to ArrayList?如何修复从 LinkedTreeMap 到 ArrayList 的 Gson JSON 解析?
【发布时间】:2017-02-06 15:42:19
【问题描述】:

我正在使用 Retrofit+Gson 来解析 JSON。

当我尝试解析来自 Google Places API 的响应时(好吧,我不尝试解析,我只是尝试为这个响应创建模型)并且我得到了一些错误。

这是来自 Google Place API 的响应:

    {
   "predictions" : [
      {
         "description" : "Николаевская область, Украина",
         "id" : "3bd747cc4efc2288da48942b909ce18a053c2060",
         "matched_substrings" : [
            {
               "length" : 5,
               "offset" : 0
            }
         ],
         "place_id" : "ChIJydRVsbqaxUARLq1R8Q3RgpM",
         "reference" : "ClRPAAAAwseWiG8NUMt7TqSqz9rMP8R2M4rX7-cMRmIp4OCYL-VdRSr5B5T_PMwWzYOydVStVpYDvm0ldXYPEzxFAuvn1LqhtWHdROhsERwvmx0tVlwSEFdMw0sOe3rDaB2AqKKmF-YaFLvhiEOz3Bklv5-iTa7QQORILVCU",
         "structured_formatting" : {
            "main_text" : "Николаевская область",
            "main_text_matched_substrings" : [
               {
                  "length" : 5,
                  "offset" : 0
               }
            ],
            "secondary_text" : "Украина"
         },
         "terms" : [
            {
               "offset" : 0,
               "value" : "Николаевская область"
            },
            {
               "offset" : 22,
               "value" : "Украина"
            }
         ],
         "types" : [ "administrative_area_level_1", "political", "geocode" ]
      }, ...],
   "status" : "OK"
}

这是我的回复模型:

    public class GetGoogleMapPlacesResponse {
    @SerializedName("predictions")
    private List<GooglePlace> googlePlaces;

    public List<GooglePlace> getGooglePlaces() {
        return googlePlaces;
    }

    public void setGooglePlaces(List<GooglePlace> googlePlaces) {
        this.googlePlaces = googlePlaces;
    }
}

但是当 Retrofit 尝试解析对模型的响应时,我得到了错误:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.myapp.net.rest.response.GetGoogleMapPlacesResponse

这是调试模式下的原始响应:

【问题讨论】:

标签: java android json gson


【解决方案1】:

您缺少 GetGoogleMapPlacesResponse 模型的构造函数。

public class GetGoogleMapPlacesResponse {
     private List<GooglePlace> googlePlaces;
     private String status;

     public GetGoogleMapPlacesResponse(List<GooglePlace> googlePlaces, String status) {
         this.googlePlaces = googlePlaces;
         this.status = status;
     }

    ...getters & setters
}

但我强烈建议您使用带有 Gson 扩展的 AutoValue,然后您的模型将如下所示:

@AutoValue
public abstract class GetGoogleMapPlacesResponse {
      @SerializedName("predictions") public abstract List<GooglePlace> googlePlaces;
      public abstract String status;
}

更多信息请看这里:https://github.com/rharter/auto-value-gson

【讨论】:

  • 第一个解决方案对我不起作用,第二个 - 我考虑过这个,但我不想使用鬃毛库。
  • 1:导致错误的粘贴方法(您如何访问该预测列表)2:只需使用它,不变性和可读性就是要走的路
  • @mayosk Gson 不需要构造函数在反序列化期间进行实例化。 OP 面临的问题是某处的类型转换损坏。
  • @LyubomyrShaydariv 是的,但前提是您使用 gson ^2.3.1
  • @mayosk 嗯,更改日志说“无需为使用 Gson 序列化的类定义无参数构造函数”在 v1.7 中......我错过了什么吗?
猜你喜欢
  • 1970-01-01
  • 2015-03-01
  • 2019-11-02
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-14
相关资源
最近更新 更多