使用jackson mapper有选择地将JSON数据的一部分映射到嵌套类

时间:2017-09-01 08:32:09

标签: java jackson inner-classes json-deserialization mapper

我正在尝试使用jackson mapper将JSON数据映射到Java类。虽然我的JSON数据是一个扁平对象,没有嵌套,但我想将部分数据映射到其内部类

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

目前,当我反序列化JSON数据时,我拥有Parent类(安全性)变量的所有正确映射,但子类(Profit)变量未被分配

反序列化数据的快照:

{ 
  "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来做到这一点,但我还没有找到实现这个目标的方法..我的帮助将不胜感激!预先感谢。

1 个答案:

答案 0 :(得分:0)

您可以在 var margin = {top: 20, right: 100, bottom: 30, left: 40}; var svgWidth = window.innerWidth - (window.innerWidth/4); var width = svgWidth, height = (Math.min(width) / 2) + 100, radius = Math.min(width, height) / 3; var oRadius = radius, //var holding value for the outer radius of the arc iRadius = Math.min(width, height) / 4, //var holding the value for the inner radius of the arc cRadius = 8; //var holding the value for the corner radius of the arc var piePad = 5; /////////////////////////////////////////////////////////////// ////// Graphing Function /////////////////////////////////////////////////////////////// function graph(_selection) { _selection.each(function(data) { var pie = d3.pie() .padAngle(.01) .sort(null) .value(function(d) { // console.log(d.value.value) return d.value.value; }); /////////////////////////////////////////////////////////////// ////// Scales /////////////////////////////////////////////////////////////// var max = d3.max(data, function(d) { return d.value; }); var min = d3.min(data, function(d) { return d.value; }); var colorScale = setColorScale(max); /////////////////////////////////////////////////////////////// ////// Pie Vars /////////////////////////////////////////////////////////////// var pie = d3.pie() .padAngle(.01) .value(function(d) {return d.value;}) // .value(function(d) { return d[1]; }) .sort(null); var arc = d3.arc() .padRadius(oRadius + piePad) .outerRadius(oRadius - piePad) // .innerRadius(radius - (radius/2.piePad)); .innerRadius(iRadius); // .cornerRadius(cRadius); var outerArc = d3.arc() .padRadius(oRadius + piePad) .innerRadius(radius * 0.9) .outerRadius(radius * 0.9); // .cornerRadius(cRadius); var arcOut = d3.arc() .padRadius(oRadius + piePad) .innerRadius(iRadius - piePad*4) .outerRadius(oRadius - piePad); // .cornerRadius(cRadius); var arcOver = d3.arc() .padRadius(oRadius + piePad) .innerRadius(iRadius - piePad*2) .outerRadius(oRadius - piePad*2); /////////////////////////////////////////////////////////////// ////// Build Initial SVG /////////////////////////////////////////////////////////////// if (!svg){ svg = d3.select(this).append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("class", "donut-group") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); } /////////////////////////////////////////////////////////////// ////// Add paths for pie /////////////////////////////////////////////////////////////// var path = svg.selectAll("path").data(pie(data)); path.enter() .append("path") .attr("class",animatePathIn) /// rest of path code } // end graph return graph; } 类本身上创建相应的setter,将其映射到嵌套的Security对象。

以下是ProfitSecurity的修改后的类。

Profit

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

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 + "]";
    }
}