怎么用java对象表示复杂的json?
威尼斯人线上娱乐官网
威尼斯人线上娱乐官网
当前位置 : 威尼斯人线上娱乐官网 > 澳门威尼斯人娱乐城

怎么用java对象表示复杂的json?

有种json对象要大量使用,所以想封装成对象,

{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]}}

现在的想法是一层对象再套一层tooltip,xAxis,series对象,可是完全偏离目标了,本来就只是一个辅助对象。有没有什么好的做法?

虽然并没有太理解你的问题,不过针对json去写一些类表示这个json的结构,是有点烦...不过有个神器可以帮你简单解决这个问题,哈哈

IDEA里有个神器叫GsonFormat

这个插件处理json十分好用,不管json有多复杂,只要json格式正确,自动帮你生成类,哈哈

一般使用步骤是这样的:

  1. 新建一个类,比如叫Test,这个类就是你最后要使用的类

  1. 然后Alt+s 打开GsonFormat的快捷键

  1. 把要转换的json字符串粘贴进去,点ok

4.确认转换后的格式和类型,基本默认都可以的,直接点ok即可

  1. 类生成:

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 setTooltipTooltipBean tooltip {
        this.tooltip = tooltip;
    }

    public XAxisBean getXAxis {
        return xAxis;
    }

    public void setXAxisXAxisBean xAxis {
        this.xAxis = xAxis;
    }

    public YAxisBean getYAxis {
        return yAxis;
    }

    public void setYAxisYAxisBean yAxis {
        this.yAxis = yAxis;
    }

    public SeriesBean getSeries {
        return series;
    }

    public void setSeriesSeriesBean 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 setTriggerString trigger {
            this.trigger = trigger;
        }

        public AxisPointerBean getAxisPointer {
            return axisPointer;
        }

        public void setAxisPointerAxisPointerBean axisPointer {
            this.axisPointer = axisPointer;
        }

        public String getFormatter {
            return formatter;
        }

        public void setFormatterString formatter {
            this.formatter = formatter;
        }

        public static class AxisPointerBean {
            /**
             * type : cross
             */

            private String type;

            public String getType {
                return type;
            }

            public void setTypeString 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 setTypeString type {
            this.type = type;
        }

        public AxisTickBean getAxisTick {
            return axisTick;
        }

        public void setAxisTickAxisTickBean axisTick {
            this.axisTick = axisTick;
        }

        public List<String> getData {
            return data;
        }

        public void setDataList<String> data {
            this.data = data;
        }

        public static class AxisTickBean {
            /**
             * alignWithLabel : true
             */

            private boolean alignWithLabel;

            public boolean isAlignWithLabel {
                return alignWithLabel;
            }

            public void setAlignWithLabelboolean alignWithLabel {
                this.alignWithLabel = alignWithLabel;
            }
        }
    }

    public static class YAxisBean {
        /**
         * type : value
         */

        private String type;

        public String getType {
            return type;
        }

        public void setTypeString 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 setTypeString type {
            this.type = type;
        }

        public String getBarWidth {
            return barWidth;
        }

        public void setBarWidthString barWidth {
            this.barWidth = barWidth;
        }

        public List<Integer> getData {
            return data;
        }

        public void setDataList<Integer> data {
            this.data = data;
        }
    }
}
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 setTriggerString trigger {
            this.trigger = trigger;
        }
        public Map<String, String> getAxisPointer {
            return axisPointer;
        }
        public void setAxisPointerMap<String, String> axisPointer {
            this.axisPointer = axisPointer;
        }
        public String getFormatter {
            return formatter;
        }
        public void setFormatterString formatter {
            this.formatter = formatter;
        }
    }

    public static void mainString[] args {
        MM mm = new MM;
        Tooltip tooltip = mm.new Tooltip;
        tooltip.setTrigger"axis";
        tooltip.setAxisPointernew 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.printlnJSON.toJSONStringmap;
    }

}

输出:

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

广告位

威尼斯人线上娱乐官网