使用杰克森映射器选择性地将JSON数据的一部分映射到嵌套类

问题描述:

我想使用杰克逊映射器将JSON数据映射到Java类。虽然我的JSON数据是一个平面对象,没有嵌套,我想映射部分数据到它的内部类使用杰克森映射器选择性地将JSON数据的一部分映射到嵌套类

为了说明我的观点,如果您查看下面的JSON数据,security_namemarket_cap字段将直接映射到Security class。 但1_month_profit3_month_profit6_month_profit字段需要映射到一个内部类 - 。Profit class(如1_month_profit的利润类的private Double oneMonthProfit

目前,当我反序列化JSON数据,我拥有所有的正确映射父类(安全)的变量,但儿童类(利润)变量没有被分配

反序列化数据的快照:

{ 
    "security_name": "Apple", 
    "market_cap": 13,000,000,000, 
    "profit": { 
    "1_month_profit": null, // <-- not being assigned.. 
    "3_month_profit": null, // <-- not being assigned.. 
    "6_month_profit": null // <-- not being assigned.. 
    }, 
    ... 
} 

JSON数据如下:

{ 
    "security_name": "Apple", 
    "market_cap": 13,000,000,000, 
    "1_month_profit": 1.2, 
    "3_month_profit": -2.0, 
    "6_month_profit": 3.0 
     ... 
} 

安全类整个JSON数据映射如下:

public class Security { 
    private String securityName; 
    private Integer marketCap; 
    private Profit profit = new Profit(); 

    public String getSecurityName() { 
    return securityName; 
    } 

    @JsonProperty("security_name") 
    public void setSecurityName(String securityName) { 
    this.securityName = securityName; 
    } 

    public Integer getMarketCap() { 
    return marketCap; 
    } 

    @JsonProperty("market_cap") 
    public void setMarketCap(String marketCap) { 
    this.marketCap= marketCap; 
    } 

    @JsonProperty("profit") 
    public Profit getProfit() { 
    return profit; 
    } 

    public class Profit { 
    private Double oneMonthProfit; 
    private Double threeMonthProfit; 
    private Double sixMonthProfit; 

    public Double getOneMonthProfit() { 
     return oneMonthProfit; 
    } 

    @JsonProperty("1_month_profit") // <-- this has no effect. 
    public void setOneMonthProfit(Double oneMonthProfit) { 
     this.oneMonthProfit = oneMonthProfit; 
    } 


    public Double getThreeMonthProfit() { 
     return threeMonthProfit; 
    } 

    @JsonProperty("3_month_profit") 
    public void setThreeMonthProfit(Double threeMonthProfit) { 
     this.threeMonthProfit = threeMonthProfit; 
    } 


    public Double getSixMonthProfit() { 
     return sixMonthProfit; 
    } 

    @JsonProperty("6_month_profit") 
    public void setSixMonthProfit(Double sixMonthProfit) { 
     this.sixMonthProfit = sixMonthProfit; 
    } 
    } 
} 

我希望加入@JsonProperty标注在内部类将解决问题,但不幸的是这并没有任何效果。

我觉得必须有一种方法来使用jackson mapper来做到这一点,但我还没有找到一种方法来实现这一点呢..你的帮助将非常感谢!预先感谢。

+0

你能否详细说明你的问题。此结构看起来正确 – 2017-09-01 08:37:07

+0

如果您查看反序列化数据(上面)与当前结构的结果,则“利润”字段下的嵌套字段具有空值。我希望能够将所有字段的值映射到利润内。 –

+0

参考下面的答案,它会正确地映射数据 – 2017-09-01 09:02:03

您可以在Security类本身上创建相应的setter,将其映射到嵌套的Profit对象。

这里是SecurityProfit的修改后的类。

class Security { 
    private String securityName; 
    private Integer marketCap; 
    private Profit profit = new Profit(); 

    public String getSecurityName() { 
     return securityName; 
    } 

    @JsonProperty("security_name") 
    public void setSecurityName(String securityName) { 
     this.securityName = securityName; 
    } 

    public Integer getMarketCap() { 
     return marketCap; 
    } 

    @JsonProperty("market_cap") 
    public void setMarketCap(Integer marketCap) { 
     this.marketCap = marketCap; 
    } 

    @JsonProperty("profit") 
    public Profit getProfit() { 
     return profit; 
    } 

    @JsonProperty("1_month_profit") 
    public void setOneMonthProfit(Double oneMonthProfit) { 
     this.profit.oneMonthProfit = oneMonthProfit; 
    } 

    @JsonProperty("3_month_profit") 
    public void setThreeMonthProfit(Double threeMonthProfit) { 
     this.profit.threeMonthProfit = threeMonthProfit; 
    } 

    @JsonProperty("6_month_profit") 
    public void setSixMonthProfit(Double sixMonthProfit) { 
     this.profit.sixMonthProfit = sixMonthProfit; 
    } 

    class Profit { 
     private Double oneMonthProfit; 

     private Double threeMonthProfit; 

     private Double sixMonthProfit; 

     @Override 
     public String toString() { 
      return "Profit [oneMonthProfit=" + oneMonthProfit + ", threeMonthProfit=" + threeMonthProfit 
        + ", sixMonthProfit=" + sixMonthProfit + "]"; 
     } 
    } 

    @Override 
    public String toString() { 
     return "Security [securityName=" + securityName + ", marketCap=" + marketCap + ", profit=" + profit + "]"; 
    } 
} 

然后你有设置的值。这是我跑步的输出。

Security [securityName=Apple, marketCap=130000, profit=Profit [oneMonthProfit=1.2, threeMonthProfit=-2.0, sixMonthProfit=3.0]]