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