我有一个类似于以下内容的JSON响应:
Subscription POJO类及其内部是“ subscriptionPlans”的数组列表:
SubscriptionDetails.java
email XOXA
name XOXA
SubscriptionPlans.java
@Expose()
@SerializedName("subscriptionPlans")
public ArrayList<SubscriptionPlans> subscriptionPlans;
public ArrayList<SubscriptionPlans> getSubscriptionPlans() {
return subscriptionPlans;
}
@Override
public String toString() {
return "SubscriptionDetails{" +
"subscriptionPlans=" + subscriptionPlans +
'}';
}
我正在使用Gson从JSON获取数据,并将其填充到各种POJO类中,如下所示:
@SerializedName("plan_name")
@Expose
public String planName;
@SerializedName("description")
@Expose
public String description;
@SerializedName("amount")
@Expose
public String amount;
public String getPlanName() {
return planName;
}
public String getDescription() {
return description;
}
public String getAmount() {
return amount;
}
但是,我得到了错误响应,
Gson gson = new Gson();
SubscriptionDetails subscriptionDetails = gson.fromJson(String.valueOf(jsonObject.getJSONArray("subscriptionPlans")), SubscriptionDetails.class);
ArrayList<SubscriptionPlans> subscriptionPlans = subscriptionDetails.getSubscriptionPlans();
String amount = subscriptionPlans.get(0).getAmount();
我在这里缺少什么或做错了什么?
答案 0 :(得分:3)
将整个字符串传递给GSON,而不仅仅是String.valueOf(jsonObject.getJSONArray("subscriptionPlans"))
:
SubscriptionDetails subscriptionDetails = gson.fromJson(String.valueOf(jsonObject), SubscriptionDetails.class);