Home  >  Q&A  >  body text

How to use java objects to represent complex json?

There is a json object that needs to be used a lot, so I want to encapsulate it into an object,

{tooltip : {
          trigger: 'axis',
          axisPointer: {type: 'cross',},
          formatter: "{b}: {c})"},
xAxis :{
      type : 'category',
      data : ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      axisTick: {alignWithLabel: true}},
yAxis :{
      type : 'value'},
series :{
      type:'bar',
      barWidth: '80%',
      data:[10, 52, 200, 334, 390, 330, 220]}}

The current idea is to put one layer of objects and another layer of tooltip, xAxis, and series objects, but it is completely off target. It is just an auxiliary object. Are there any good practices?

给我你的怀抱给我你的怀抱2735 days ago599

reply all(2)I'll reply

  • 滿天的星座

    滿天的星座2017-05-17 10:10:15

    Although I don’t quite understand your problem, it is a bit annoying to write some classes for json to represent the structure of this json... But there is an artifact that can help you solve this problem simply, haha ​​

    There is an artifact in IDEA called GsonFormat

    This plug-in is very easy to use for processing json. No matter how complex the json is, as long as the json format is correct, it will automatically generate classes for you, haha ​​

    General usage steps are as follows:

    1. Create a new class, for example, called Test. This class will be the last class you want to use

    1. Then Alt+s to open the GsonFormat shortcut key

    1. Paste the json string you want to convert and click ok

    4. Confirm the format and type after conversion. Basically, the default is OK. Just click OK

    1. Class generation:

    public class Test {
        /**
         * tooltip : {"trigger":"axis","axisPointer":{"type":"cross"},"formatter":"{b}: {c})"}
         * xAxis : {"type":"category","data":["Mon","Tue","Wed","Thu","Fri","Sat","Sun"],"axisTick":{"alignWithLabel":true}}
         * yAxis : {"type":"value"}
         * series : {"type":"bar","barWidth":"80%","data":[10,52,200,334,390,330,220]}
         */
    
        private TooltipBean tooltip;
        private XAxisBean xAxis;
        private YAxisBean yAxis;
        private SeriesBean series;
    
        public TooltipBean getTooltip() {
            return tooltip;
        }
    
        public void setTooltip(TooltipBean tooltip) {
            this.tooltip = tooltip;
        }
    
        public XAxisBean getXAxis() {
            return xAxis;
        }
    
        public void setXAxis(XAxisBean xAxis) {
            this.xAxis = xAxis;
        }
    
        public YAxisBean getYAxis() {
            return yAxis;
        }
    
        public void setYAxis(YAxisBean yAxis) {
            this.yAxis = yAxis;
        }
    
        public SeriesBean getSeries() {
            return series;
        }
    
        public void setSeries(SeriesBean series) {
            this.series = series;
        }
    
        public static class TooltipBean {
            /**
             * trigger : axis
             * axisPointer : {"type":"cross"}
             * formatter : {b}: {c})
             */
    
            private String trigger;
            private AxisPointerBean axisPointer;
            private String formatter;
    
            public String getTrigger() {
                return trigger;
            }
    
            public void setTrigger(String trigger) {
                this.trigger = trigger;
            }
    
            public AxisPointerBean getAxisPointer() {
                return axisPointer;
            }
    
            public void setAxisPointer(AxisPointerBean axisPointer) {
                this.axisPointer = axisPointer;
            }
    
            public String getFormatter() {
                return formatter;
            }
    
            public void setFormatter(String formatter) {
                this.formatter = formatter;
            }
    
            public static class AxisPointerBean {
                /**
                 * type : cross
                 */
    
                private String type;
    
                public String getType() {
                    return type;
                }
    
                public void setType(String type) {
                    this.type = type;
                }
            }
        }
    
        public static class XAxisBean {
            /**
             * type : category
             * data : ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
             * axisTick : {"alignWithLabel":true}
             */
    
            private String type;
            private AxisTickBean axisTick;
            private List<String> data;
    
            public String getType() {
                return type;
            }
    
            public void setType(String type) {
                this.type = type;
            }
    
            public AxisTickBean getAxisTick() {
                return axisTick;
            }
    
            public void setAxisTick(AxisTickBean axisTick) {
                this.axisTick = axisTick;
            }
    
            public List<String> getData() {
                return data;
            }
    
            public void setData(List<String> data) {
                this.data = data;
            }
    
            public static class AxisTickBean {
                /**
                 * alignWithLabel : true
                 */
    
                private boolean alignWithLabel;
    
                public boolean isAlignWithLabel() {
                    return alignWithLabel;
                }
    
                public void setAlignWithLabel(boolean alignWithLabel) {
                    this.alignWithLabel = alignWithLabel;
                }
            }
        }
    
        public static class YAxisBean {
            /**
             * type : value
             */
    
            private String type;
    
            public String getType() {
                return type;
            }
    
            public void setType(String type) {
                this.type = type;
            }
        }
    
        public static class SeriesBean {
            /**
             * type : bar
             * barWidth : 80%
             * data : [10,52,200,334,390,330,220]
             */
    
            private String type;
            private String barWidth;
            private List<Integer> data;
    
            public String getType() {
                return type;
            }
    
            public void setType(String type) {
                this.type = type;
            }
    
            public String getBarWidth() {
                return barWidth;
            }
    
            public void setBarWidth(String barWidth) {
                this.barWidth = barWidth;
            }
    
            public List<Integer> getData() {
                return data;
            }
    
            public void setData(List<Integer> data) {
                this.data = data;
            }
        }
    }

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-17 10:10:15

    import java.util.HashMap;
    import java.util.Map;
    
    import com.alibaba.fastjson.JSON;
    
    public class MM {
        class Tooltip {
            private String trigger;
            private Map<String, String> axisPointer;
            private String formatter;
            public String getTrigger() {
                return trigger;
            }
            public void setTrigger(String trigger) {
                this.trigger = trigger;
            }
            public Map<String, String> getAxisPointer() {
                return axisPointer;
            }
            public void setAxisPointer(Map<String, String> axisPointer) {
                this.axisPointer = axisPointer;
            }
            public String getFormatter() {
                return formatter;
            }
            public void setFormatter(String formatter) {
                this.formatter = formatter;
            }
        }
    
        public static void main(String[] args) {
            MM mm = new MM();
            Tooltip tooltip = mm.new Tooltip();
            tooltip.setTrigger("axis");
            tooltip.setAxisPointer(new HashMap<String, String>(){{
                this.put("type", "cross");
            }});
            tooltip.setFormatter("{b}: {c})");
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("tooltip", tooltip);
            
            System.out.println(JSON.toJSONString(map));
        }
    
    }

    Output:

    {"tooltip":{"axisPointer":{"type":"cross"},"formatter":"{b}: {c})","trigger":"axis"}}

    reply
    0
  • Cancelreply