如何使用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 条评论
登录 后参与评论

相关文章

在Jackson中反序列化具有相同名称但不同类型的属性?

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

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

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

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

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

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

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

如何将具有不同名称空间的相同XML元素反序列化为结构中的不同元素

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

使用Java中的Jackson将序列化具有不同类型值的映射到JSON

使用包含变量类型字段的Gson反序列化JSON响应

使用杰克逊从改造中反序列化json,其中相同的变量名称可以表示两个不同的对象

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

如何在Jackson中对具有抽象类型的引用进行反序列化JSON

如何反序列化具有包含不同对象的属性的json?

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

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

当序列化具有与该对象相同类型的类成员的对象时,如何解决循环引用

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

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

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

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

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

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

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

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

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

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

TOP 榜单

  1. 1

    来自Microsoft Office加载项taskpane.js的MySQL驱动程序模块的空引用

  2. 2

    使用AWS Cognito和React的仅限Facebook / Google的登录名(无用户名/密码)

  3. 3

    创建Windows Phone 8应用并将其连接到数据库的最佳方法(最好是SQL Server)

  4. 4

    为什么Java中的System.out.println()打印到控制台?

  5. 5

    卷曲函数无法解析来自bash中变量的代理

  6. 6

    是什么在Android的consumer-rules.pro和proguard-rules.pro之间的区别?

  7. 7

    设置与Apache POI Excel表散点图标记图标的颜色

  8. 8

    将Qt Pyside2与asyncio await语法一起使用?

  9. 9

    崇高的文字+蟒蛇的蟒蛇

  10. 10

    任务':app:minifyReleaseWithR8'.java.lang.NullPointerException的执行失败(无错误消息)

  11. 11

    OpenJDK的和AdoptOpenJDK的区别

  12. 12

    大型数据集缓存到Spark内存中时,“超出了GC开销限制”(通过sparklyr和RStudio)

  13. 13

    “执行测试CMAKE_HAVE_LIBC_PTHREAD”失败实际上是什么意思?

  14. 14

    使用Core 2.2中的Identity,如何在关闭浏览器15分钟后保持会话活动?

  15. 15

    React中的ForwardRefExoticComponent和ForwardRefRenderFunction有什么区别?

  16. 16

    猫鼬查找结果,然后将字段替换为findOne

  17. 17

    如何降级Google Colab的Torch版本

  18. 18

    Keras提前停止回调错误,val_loss指标不可用

  19. 19

    如何避免VSCode中的“导入路径不能以.ts扩展名结尾”错误?

  20. 20

    Nuxt.JS:如何在页面中获取路由URL参数

  21. 21

    是否有为什么会AccessibilityManager.sInstance导致内存泄漏的一个原因?

热门标签

归档