JAVA druid 查询语句Json构造
Json原型:
{ "fields": [ { "dimension": "AA", "type": "or", "value": "11" }, { "fields": [ { "dimension": "BB", "type": "or", "value": "22" }, { "dimension": "CC", "type": "or", "value": "33" } ], "type": "and" } ], "type": "and" }
fields 类(外层类):
package com.json; import java.util.List; import java.util.Map; public class fields { private String type; private List<Object> fields; public String getType() { return type; } public void setType(String type) { this.type = type; } public List<Object> getFields() { return fields; } public void setFields(List<Object> fields) { this.fields = fields; } }fields 类(内层类):
package com.json3; public class fields { private String type; private String dimension; private String value; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getDimension() { return dimension; } public void setDimension(String dimension) { this.dimension = dimension; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
Main 方法构造输出:
package com.json; import java.util.ArrayList; import java.util.List; import net.sf.json.JSONObject; public class Main { public void setSturct() { fields fieldA = new fields(); fields fieldB = new fields(); List<Object> modelA30 = new ArrayList<>(); com.json3.fields fieldObjectA30 = new com.json3.fields(); fieldObjectA30.setType("or"); fieldObjectA30.setDimension("AA"); fieldObjectA30.setValue("11"); modelA30.add(fieldObjectA30); ////////////////////////////////////////////////// List<Object> modelA31 = new ArrayList<>(); com.json3.fields fieldObjectA31 = new com.json3.fields(); fieldObjectA31.setType("or"); fieldObjectA31.setDimension("BB"); fieldObjectA31.setValue("22"); com.json3.fields fieldObjectA32 = new com.json3.fields(); fieldObjectA32.setType("or"); fieldObjectA32.setDimension("CC"); fieldObjectA32.setValue("33"); modelA31.add(fieldObjectA31); modelA31.add(fieldObjectA32); fieldB.setType("and"); fieldB.setFields(modelA31); modelA30.add(fieldB); /////////////////////////////////////////////////////// fieldA.setType("and"); fieldA.setFields(modelA30); String result = JSONObject.fromObject(fieldA).toString(); System.out.println(result); } public static void main(String[] args) { // TODO Auto-generated method stub new Main().setSturct(); } }
输出结果:
{ "fields": [ { "dimension": "AA", "type": "or", "value": "11" }, { "fields": [ { "dimension": "BB", "type": "or", "value": "22" }, { "dimension": "CC", "type": "or", "value": "33" } ], "type": "and" } ], "type": "and" }
总结不好多多担待,文章只单纯个人总结,如不好勿喷,技术有限,有错漏麻烦指正提出。本人QQ:373965070