思路:首先将JSON格式的数据转换成JSONObject,然后将JSONObject转换成Java的实体类(其中类属性包括List等类型)
Java实体类:
SearchFilter 类
1 public class SearchFilter {2private String groupOp;34private List<SearchRule> rules;5 6public String getGroupOp() {7 return groupOp;8}9 10public void setGroupOp(String groupOp) {11 this.groupOp = groupOp;12}13 14public List<SearchRule> getRules() {15 return rules;16}17 18public void setRules(List<SearchRule> rules) {19 this.rules = rules;20}
SearchRule 类:
1 public class SearchRule {2private String field;3private String op;4private String data;5public String getField() {6 return field;7}8public void setField(String field) {9 this.field = field;10}11public String getOp() {12 return op;13}14public void setOp(String op) {15 this.op = op;16}17public String getData() {18 return data;19}20public void setData(String data) {21 this.data = data;22}23 24 25 }
1 //导入的package2 import com.fasterxml.jackson.databind.ObjectMapper;3 import net.sf.json.JSONArray;4 import net.sf.json.JSONObject;
解析JSon格式字符串的方法
1 public SearchFilter jsonToSearchFilter(String filters) {2 SearchFilter searchFilter = null;3 try {4 JSONObject jsonobject = JSONObject.fromObject(filters);//将json格式的字符串转换成JSONObject 对象5 JSONArray array = jsonobject.getJSONArray("rules"); //如果json格式的字符串里含有数组格式的属性,将其转换成JSONArray,以方便后面转换成对应的实体6 List<SearchRule> rules = new ArrayList<SearchRule>();7 for (int i = 0; i < array.size(); i++) {8 JSONObject object = (JSONObject) array.get(i);//将array中的数据进行逐条转换9 SearchRule rule = (SearchRule) JSONObject.toBean(object, SearchRule.class); //通过JSONObject.toBean()方法进行对象间的转换10 rules.add(rule);11 }12 String groupOp = jsonobject.getString("groupOp"); //简单的直接获取值13 searchFilter = new SearchFilter(); //对SearchFilter对象进行组装14 searchFilter.setGroupOp(groupOp);15 searchFilter.setRules(rules);16 } catch (Exception e) {17 System.out.println("filters=" + filters.toString() + ".json转换成实体类出错");18 e.printStackTrace();19 }20 return searchFilter;21}
这里只进行了SearchFilter类中list<SearchRule>rule属性的对象转换,因为它是一个SearchRule对象数组,需要单独拿出来进行json转换成对象,如果直接对filter进行 SearchFilter进行实体转换会报错。如果对象中不含有list,array,map等集合,可以像8-9行一样进行对象转换。
测试:
1 public static void main(String[] args) {2 UserMgmtController con = new UserMgmtController();3 String filters="{\"groupOp\": \"OR\",\"rules\": [{\"field\": \"realname\",\"op\": \"eq\","+4 "\"data\": \"1234\"},{\"field\": \"cityCode\",\"op\": \"ne\",\"data\": \"5678\"}]}";5 SearchFilter searchFilter =con.jsonToSearchFilter(filters);6 System.out.println("gop="+searchFilter.getGroupOp()+" rules.size="+searchFilter.getRules().size());7}结果:gop=OR rules.size=2
将java对象转换成json格式:
1 //将Object 对象转换成 json2public String objectToJson(Object obj) {3 ObjectMapper mapper = new ObjectMapper();4 // Convert object to JSON string5 String jsonStr = null;6 try {7 jsonStr = mapper.writeValueAsString(obj);8 } catch (IOException e) {9 log.error(obj + "entity 轉換成 json出錯", e);10 e.printStackTrace();11 System.out.println("entity 轉換成 json出錯");12 }13 return jsonStr;14}
将Object 转换成json字符串时,默认使用用是的驼峰方式,如果不想使用驼峰则可以增加红色部分设置,带下划线的也可以原样输出:
1 public String objectToJson(Object obj){2 ObjectMapper mapper = new ObjectMapper();3 String json =null;4 try {5 PropertyNamingStrategy.PascalCaseStrategy pp = new PropertyNamingStrategy.PascalCaseStrategy(); 6 mapper.setPropertyNamingStrategy(pp);7 json = mapper.writeValueAsString(obj);8 } catch (JsonProcessingException e) {9 log.error("json对象转换失败",e);10 e.printStackTrace();11 }12 return json;13}
结果:
"UpLoadDateTime":"-09-25 10:51:41","OrganizationId":"B3EA5A4D-7C60-4C21-AF09-494890467C34" ,"Test_Code":""
来源:/smallfa/p/10495508.html