如何使用Retrofit / Gson在JSON中反序列化相同类型但具有不同字段名称的对象列表?

时间:2018-12-30 10:58:28

标签: android json serialization gson retrofit2

我当前使用的REST API构造得不太好。 JSON对象quarterly应该在内部具有对象列表或对象数组,但它仅具有按日期可寻址的对象(“ 2018-09-29”等)。

我正在使用Retrofit2将此JSON提取到POJO类中,但是我当前的实现无法正常工作,因为它需要JSON列表。如果有人可以帮助我了解如何更改Java,以便将所有季度对象存储在Java列表中,我将不胜感激。

当前CashFlowRecordList类中的列表为null-即。进行REST调用时不会填充它。

我的问题与Convert json structure to array with Retrofit几乎相同。但是它仍然不能回答如何使用翻新。

JSON:

Cash_Flow": {
      "currency_symbol": "USD",
      "quarterly": {
        "2018-09-29": {
          "date": "2018-09-29",
          "filing_date": "2018-11-05",
          "investments": "552000000.00",
          "changeToLiabilities": "20373000000.00",
          "totalCashflowsFromInvestingActivities": "-3001000000.00",
          "netBorrowings": "-27000000.00",
          "totalCashFromFinancingActivities": "-22580000000.00",
          "changeToOperatingActivities": "-12423000000.00",
          "netIncome": "14125000000.00",
          "changeInCash": "-6058000000.00",
        },
        "2018-06-30": {
          "date": "2018-06-30",
          "filing_date": "2018-08-01",
          "investments": "7916000000.00",
          "changeToLiabilities": "1500000000.00",
          "totalCashflowsFromInvestingActivities": "3947000000.00",
          "netBorrowings": "-6011000000.00",
          "totalCashFromFinancingActivities": "-31523000000.00",
          "changeToOperatingActivities": "-5340000000.00",
          "netIncome": "11519000000.00",
          "changeInCash": "-13088000000.00",
        },
        ...
    }
}

JAVA:

现金流

public class CashFlow {

    public CashFlow(CashFlowRecordList quarterly, CashFlowRecordList yearly) {
        this.quarterly = quarterly;
        this.yearly = yearly;
        //
    }

    public CashFlow() {
        // TODO Auto-generated constructor stub
    }


    @SerializedName("quarterly")
    @Expose private CashFlowRecordList quarterly;

    @SerializedName("yearly")
    @Expose private CashFlowRecordList yearly;

    public CashFlowRecordList getQuarterly() {
        return quarterly;
    }

    public void setQuarterly(CashFlowRecordList quarterly) {
        this.quarterly = quarterly;
    }

    public CashFlowRecordList getYearly() {
        return yearly;
    }

    public void setYearly(CashFlowRecordList yearly) {
        this.yearly = yearly;
    }

}

CashFlowRecordList

public class CashFlowRecordList {

    @Expose public CashFlowRecord cashFlowRecord[];

    public CashFlowRecordList() {
        // TODO Auto-generated constructor stub
    }

    public CashFlowRecordList(CashFlowRecord[] cashFlowRecord){
        this.cashFlowRecord = cashFlowRecord;

    }

    public CashFlowRecord[] getCashFlowRecord() {
        return cashFlowRecord;
    }

    public void setCashFlowRecord(CashFlowRecord[] cashFlowRecord) {
        this.cashFlowRecord = cashFlowRecord;
    }
}

现金流量记录

public class CashFlowRecord {

    @SerializedName("date")
    @Expose
    private String date;
    @SerializedName("investments")
    @Expose
    private String investments;
    @SerializedName("changeToLiabilities")
    @Expose
    private String changeToLiabilities;
    @SerializedName("totalCashflowsFromInvestingActivities")
    @Expose
    private String totalCashflowsFromInvestingActivities;
    @SerializedName("netBorrowings")
    @Expose
    private String netBorrowings;
    @SerializedName("totalCashFromFinancingActivities")
    @Expose
    private String totalCashFromFinancingActivities;
    @SerializedName("changeToOperatingActivities")
    @Expose
    private String changeToOperatingActivities;
    @SerializedName("netIncome")
    @Expose
    private String netIncome;
    @SerializedName("changeInCash")
    @Expose
    private String changeInCash;
    @SerializedName("totalCashFromOperatingActivities")
    @Expose
    private String totalCashFromOperatingActivities;
    @SerializedName("depreciation")
    @Expose
    private String depreciation;
    @SerializedName("otherCashflowsFromInvestingActivities")
    @Expose
    private String otherCashflowsFromInvestingActivities;
    @SerializedName("dividendsPaid")
    @Expose
    private String dividendsPaid;
    @SerializedName("changeToInventory")
    @Expose
    private String changeToInventory;
    @SerializedName("changeToAccountReceivables")
    @Expose
    private String changeToAccountReceivables;
    @SerializedName("salePurchaseOfStock")
    @Expose
    private String salePurchaseOfStock;
    @SerializedName("otherCashflowsFromFinancingActivities")
    @Expose
    private String otherCashflowsFromFinancingActivities;
    @SerializedName("changeToNetincome")
    @Expose
    private String changeToNetincome;
    @SerializedName("capitalExpenditures")
    @Expose
    private String capitalExpenditures;

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getInvestments() {
        return investments;
    }

    public void setInvestments(String investments) {
        this.investments = investments;
    }

    public String getChangeToLiabilities() {
        return changeToLiabilities;
    }

    public void setChangeToLiabilities(String changeToLiabilities) {
        this.changeToLiabilities = changeToLiabilities;
    }

    public String getTotalCashflowsFromInvestingActivities() {
        return totalCashflowsFromInvestingActivities;
    }

    public void setTotalCashflowsFromInvestingActivities(String totalCashflowsFromInvestingActivities) {
        this.totalCashflowsFromInvestingActivities = totalCashflowsFromInvestingActivities;
    }

    public String getNetBorrowings() {
        return netBorrowings;
    }

    public void setNetBorrowings(String netBorrowings) {
        this.netBorrowings = netBorrowings;
    }

    public String getTotalCashFromFinancingActivities() {
        return totalCashFromFinancingActivities;
    }

    public void setTotalCashFromFinancingActivities(String totalCashFromFinancingActivities) {
        this.totalCashFromFinancingActivities = totalCashFromFinancingActivities;
    }

    public String getChangeToOperatingActivities() {
        return changeToOperatingActivities;
    }

    public void setChangeToOperatingActivities(String changeToOperatingActivities) {
        this.changeToOperatingActivities = changeToOperatingActivities;
    }

    public String getNetIncome() {
        return netIncome;
    }

    public void setNetIncome(String netIncome) {
        this.netIncome = netIncome;
    }

    public String getChangeInCash() {
        return changeInCash;
    }

    public void setChangeInCash(String changeInCash) {
        this.changeInCash = changeInCash;
    }

    public String getTotalCashFromOperatingActivities() {
        return totalCashFromOperatingActivities;
    }

    public void setTotalCashFromOperatingActivities(String totalCashFromOperatingActivities) {
        this.totalCashFromOperatingActivities = totalCashFromOperatingActivities;
    }

    public String getDepreciation() {
        return depreciation;
    }

    public void setDepreciation(String depreciation) {
        this.depreciation = depreciation;
    }

    public String getOtherCashflowsFromInvestingActivities() {
        return otherCashflowsFromInvestingActivities;
    }

    public void setOtherCashflowsFromInvestingActivities(String otherCashflowsFromInvestingActivities) {
        this.otherCashflowsFromInvestingActivities = otherCashflowsFromInvestingActivities;
    }

    public String getDividendsPaid() {
        return dividendsPaid;
    }

    public void setDividendsPaid(String dividendsPaid) {
        this.dividendsPaid = dividendsPaid;
    }

    public String getChangeToInventory() {
        return changeToInventory;
    }

    public void setChangeToInventory(String changeToInventory) {
        this.changeToInventory = changeToInventory;
    }

    public String getChangeToAccountReceivables() {
        return changeToAccountReceivables;
    }

    public void setChangeToAccountReceivables(String changeToAccountReceivables) {
        this.changeToAccountReceivables = changeToAccountReceivables;
    }

    public String getSalePurchaseOfStock() {
        return salePurchaseOfStock;
    }

    public void setSalePurchaseOfStock(String salePurchaseOfStock) {
        this.salePurchaseOfStock = salePurchaseOfStock;
    }

    public String getOtherCashflowsFromFinancingActivities() {
        return otherCashflowsFromFinancingActivities;
    }

    public void setOtherCashflowsFromFinancingActivities(String otherCashflowsFromFinancingActivities) {
        this.otherCashflowsFromFinancingActivities = otherCashflowsFromFinancingActivities;
    }

    public String getChangeToNetincome() {
        return changeToNetincome;
    }

    public void setChangeToNetincome(String changeToNetincome) {
        this.changeToNetincome = changeToNetincome;
    }

    public String getCapitalExpenditures() {
        return capitalExpenditures;
    }

    public void setCapitalExpenditures(String capitalExpenditures) {
        this.capitalExpenditures = capitalExpenditures;
    }

}

1 个答案:

答案 0 :(得分:1)

您在JSON中的quarterly可以反序列化为Map<String, CashFlowRecord>。因此,请每季度更改一次类型,并相应地重构其余代码。因此,您不一定需要像CashFlowRecordList这样的任何自定义List实现类。

根据您的需要,您也可以尝试Map<Date, CashFlowRecord>。如果需要将键构造为Date对象,这可能会起作用。