怎么用java對象表示復(fù)雜的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]}}
現(xiàn)在的想法是一層對象再套一層tooltip,xAxis,series對象,可是完全偏離目標(biāo)了,本來就只是一個(gè)輔助對象。有沒有什么好的做法?
問題解答
回答1:雖然并沒有太理解你的問題,不過針對json去寫一些類表示這個(gè)json的結(jié)構(gòu),是有點(diǎn)煩...不過有個(gè)神器可以幫你簡單解決這個(gè)問題,哈哈
IDEA里有個(gè)神器叫GsonFormat
這個(gè)插件處理json十分好用,不管json有多復(fù)雜,只要json格式正確,自動幫你生成類,哈哈
一般使用步驟是這樣的:
新建一個(gè)類,比如叫Test,這個(gè)類就是你最后要使用的類
然后Alt+s 打開GsonFormat的快捷鍵
把要轉(zhuǎn)換的json字符串粘貼進(jìn)去,點(diǎn)ok
4.確認(rèn)轉(zhuǎn)換后的格式和類型,基本默認(rèn)都可以的,直接點(diǎn)ok即可
類生成:
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;} }}回答2:
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)); }}
輸出:
{'tooltip':{'axisPointer':{'type':'cross'},'formatter':'{b}: {c})','trigger':'axis'}}
相關(guān)文章:
1. php - 淘寶訂單拆單表設(shè)計(jì)2. 實(shí)現(xiàn)bing搜索工具urlAPI提交3. 如何用筆記本上的apache做微信開發(fā)的服務(wù)器4. mysql優(yōu)化 - MySQL如何為配置表建立索引?5. 冒昧問一下,我這php代碼哪里出錯(cuò)了???6. MySQL主鍵沖突時(shí)的更新操作和替換操作在功能上有什么差別(如圖)7. 關(guān)于mysql聯(lián)合查詢一對多的顯示結(jié)果問題8. 數(shù)據(jù)庫 - Mysql的存儲過程真的是個(gè)坑!求助下面的存儲過程哪里錯(cuò)啦,實(shí)在是找不到哪里的問題了。9. 我在網(wǎng)址中輸入localhost/abc.php顯示的是not found是為什么呢?10. windows誤人子弟啊
