有 json,它有一个嵌套数组payments,如何使用它来解析它retrofit?
"credits": [
{
"name": "Кредит Выгодный",
"contractNumber": 4666888,
"openedDate": "2018-02-09T21:58:57+00:00",
"nextPaymentDate": "2018-05-11T05:58:57+00:00",
"nextPaymentAmount": 8900,
"percent": 15.4,
"amount": 125000,
"currency": "rur",
"status":"active",
"payments": [
{
"paymentDate": "2018-03-09T21:58:57+00:00",
"paymentAmount": 3400,
"status": "paid"
},
{
"paymentDate": "2018-04-09T21:58:57+00:00",
"paymentAmount": 3700,
"status": "paid"
},
{
"paymentDate": "2018-05-09T21:58:57+00:00",
"paymentAmount": 4400,
"status": "skipped"
},
{
"paymentDate": "2018-06-09T21:58:57+00:00",
"paymentAmount": 4400,
"status": "future"
}
]
},
对于我credits创建class Credits { //данные (id, name, и т.д.)},如果我创建相同的payments,那么它很简单null或发生错误
java.lang.IllegalStateException:
Expected BEGIN OBJECT but was BEGIN_ARRAY at line 4 column 14 path $.result
public abstract class BaseProducts {
@NonNull
@SerializedName("name")
private String name;
@NonNull
@SerializedName("openedDate")
private Date openedDate;
@SerializedName("amount")
private Double amount;
@NonNull
@SerializedName("currency")
private String currency;
@Nullable
@SerializedName("status")
private String status;
public class Credits extends BaseProducts {
@SerializedName("contractNumber")
private Long contractNumber;
@NonNull
@SerializedName("nextPaymentDate")
private Date nextPaymentDate;
@SerializedName("nextPaymentAmount")
private Double nextPaymentAmount;
@NonNull
@SerializedName("percent")
private Double percent;
}
public class Payments extends BaseProducts {
@NonNull
@SerializedName("paymentDate")
private Date paymentDate;
@SerializedName("paymentAmount")
private Double paymentAmount;
}
public class ApiResponse<T> {
@SerializedName("resultCode")
private int resultCode;
@SerializedName("error")
private String error;
@SerializedName("result")
private T result;
}
public class ProductResponse {
@SerializedName("credits")
private List<Credits> credits = null;
@SerializedName("payments")
private List<Payments> payments = null;
}
public interface BankApi {
@GET(EnumsUrl.URL_PRODUCTS)
Single<ApiResponse<ProductResponse>> getProductsSingle();
}
一般来说,你需要使用JsonPojo,它会构建正确的结构
您需要使用生成器制作一个普通的 POJO,然后可能使用句柄对其进行编辑。然后只需用注释指示您需要绘制的内容。要访问嵌套数组
payments,您需要在其中创建嵌套类"credits"实际上,这里 是同样的问题,同样的问题。事实上,没有什么复杂的。
您只需要将 List 从 ProductResponse 类转移到 Credits 类(即使模型与 JSON 本身的结构保持一致)
产品响应类:
课程学分:
一般来说,POJO最好不要使用继承