【问题标题】:Retrofit GSON Parsing Arraylist Inside of Object改造对象内部的 GSON 解析 Arraylist
【发布时间】:2015-07-22 16:48:39
【问题描述】:

我有一个这样的 json 对象:

{
products:[
{
name:Prod1,
quantity:3
},
{
name:Prod2,
quantity:1
}
]
}

我的 gson 对象是这样的:

public class Product{

@SerializedName("name")
public String name;

@SerializedName("quantity")
public int quantity;

}

当我用这样的东西设置我的改造时

@GET("/products")
void getProducts(Callback<ArrayList<Product>> c);

显然,它会失败,因为该数组不是根对象。有没有一种简单的方法可以在解析该 ArrayList 之前强制将其深入到 json 中,还是我必须创建一个完整的 GSON 适配器来完成此操作?

【问题讨论】:

    标签: java json gson retrofit


    【解决方案1】:

    我通过创建和使用我自己的自定义反序列化器来实现这一点。代码比完整的 Gson 适配器更简单,但完成了我需要的。

    https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/JsonDeserializer.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多