将动态参数的JSON映射到POJO

时间:2016-10-06 05:44:35

标签: java android json retrofit gson

我有JSON这样的动态对象

{
 "related": {
  "0": {
    "post_id": "4869",
    "likes_count": "0",
    "product_id": "5348",
    "stock_current": "10000",
    "selling_price": "17500000.00"
  },
  "1": {
    "post_id": "4870",
    "likes_count": "0",
    "product_id": "5348",
    "stock_current": "10000",
    "selling_price": "10500000.00"
  },
  "2": {
    "post_id": "4871",
    "likes_count": "0",
    "product_id": "5348",
    "stock_current": "10000",
    "selling_price": "15500000.00"
  },
  "related_total": "33"
 }
}

如果参数除了POJO以外是动态的,并且可以超过3个对象,如何将其映射到"related_total"

通常当我有这样的json时

{
 "related": {
  "0": {
    "post_id": "4869",
    "likes_count": "0",
    "product_id": "5348",
    "stock_current": "10000",
    "selling_price": "17500000.00"
  },
  "1": {
    "post_id": "4870",
    "likes_count": "0",
    "product_id": "5348",
    "stock_current": "10000",
    "selling_price": "10500000.00"
  },
  "2": {
    "post_id": "4871",
    "likes_count": "0",
    "product_id": "5348",
    "stock_current": "10000",
    "selling_price": "15500000.00"
  }
 }
}

我的Pojo就像这样

public class PojoExample {


@SerializedName("related")
@Expose
private Map<String,Related> related;


public Map<String,Related> getRelated() {
    return related;
}


public void setRelated(Map<String,Related> related) {
    this.related = related;
}


public class Related {

    @SerializedName("post_id")
    @Expose
    private String postId;
    @SerializedName("likes_count")
    @Expose
    private String likesCount;
    @SerializedName("product_id")
    @Expose
    private String productId;
    @SerializedName("stock_current")
    @Expose
    private String stockCurrent;
    @SerializedName("selling_price")
    @Expose
    private String sellingPrice;

   // getter and setter here

 }
}

我使用POJO进行改造

0 个答案:

没有答案