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

Shane Monks O'Byrne

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

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

当前CashFlowRecordList班级中的名单null-即。进行REST调用时不会填充它。

我的问题与以下内容大致相同:使用Retrofit将json结构转换为数组但是它仍然没有回答如何使用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;
    }

}
皮尔霍

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Play的JSON序列化/反序列化动态字段名称

使用带有动态字段名称的 jackson 反序列化 json

具有不同类型的字段的 JSON 反序列化(使用 Jackson 和 Scala)

使用具有相同字段名称的对象的Gson解析json

GSON在Android中使用两种不同类型的对象反序列化列表

将对象序列化为JSON(使用Gson)。如何在UpperCamelCase中设置字段名称?

Retrofit2:如何在具有不同字段名称的对象中接收 JSON 响应?

反序列化中是否使用Bond字段名称?

使用GSON将POJO反序列化为JSON时将字段名称更改为小写?

当字段名称是诸如“ short”之类的保留关键字时,如何使用Newtonsoft反序列化JSON对象?

如何使用AssertJ递归比较具有不同字段名称但具有相同值的两个对象

如何反序列化具有两个名称相同但元素具有不同数据类型的XML?

如何序列化具有相同复杂prop类型但名称不同的对象

使用动态字段名称将JSON反序列化为C#类

如何在具有相同类型子节点的 c# 中反序列化 XML

使用lombok自定义序列化和反序列化的字段名称

具有通用类型和通用字段名称的GSON反序列化

如何反序列化具有不同类型的集合?

如何反序列化包含相同键名但使用不同类型的JSON文件(使用Google JSON)?

使用gson反序列化对象的特定JSON字段

使用 Gson 反序列化具有多种类型的列表列表

当字段名称为空时,如何使用 toList 反序列化数据表?

如何序列化对象中相同类型的对象?

如何使用GSON和Retrofit2反序列化多形状JSON响应?

Django:如何在嵌套序列化程序中更改字段名称

如何在Django的序列化程序中更改字段名称

如何在 JSON.NET 中反序列化具有嵌套对象列表的对象列表?

如何将JSON字段名称映射到不同的对象字段名称?

如何反序列化使用GSON作为单个对象或对象列表传递的字段?