将JSON数据转换成JAVA的实体类
思路:⾸先将JSON格式的数据转换成JSONObject,然后将JSONObject转换成Java的实体类(其中类属性包括List等类型)
Java实体类:
SearchFilter 类
1 public class SearchFilter {
2 private String groupOp;
3
4 private List<SearchRule> rules;
5
6 public String getGroupOp() {pfv
7 return groupOp;
8 }
9
10 public void tGroupOp(String groupOp) {
英语口语学习资料
11 upOp = groupOp;
12 }
13
14 public List<SearchRule> getRules() {
15 return rules;
16 }
17
18 public void tRules(List<SearchRule> rules) {
19 this.rules = rules;
20 }
SearchRule 类:system
1 public class SearchRule {
2 private String field;
3 private String op;
4 private String data;
5 public String getField() {
6 return field;
7 }
8 public void tField(String field) {
9 this.field = field;
10 }
11 public String getOp() {
12 return op;
13 }
14 public void tOp(String op) {
15 this.op = op;
16 }
17 public String getData() {
18 return data;
19 }
20 public void tData(String data) {
21 this.data = data;
22 }
23
24
25 }
1 //导⼊的package
2 import com.fasterxml.jackson.databind.ObjectMapper;
3 import net.sf.json.JSONArray;
4 import net.sf.json.JSONObject;
解析JSon格式字符串的⽅法
5860
1 public SearchFilter jsonToSearchFilter(String filters) {
2 SearchFilter archFilter = null;
3 try {
4 JSONObject jsonobject = JSONObject.fromObject(filters);//将json格式的字符串转换成JSONObject 对象
5 JSONArray array = JSONArray("rules"); //如果json格式的字符串⾥含有数组格式的属性,将其转换成JSONArray,以⽅便后⾯转换成对应的实体
6 List<SearchRule> rules = new ArrayList<SearchRule>();
7 for (int i = 0; i < array.size(); i++) {
8 JSONObject object = (JSONObject) (i); //将array中的数据进⾏逐条转换
9 SearchRule rule = (SearchRule) Bean(object, SearchRule.class); //通过Bean()⽅法进⾏对象间的转换
10 rules.add(rule);
11 }
12 String groupOp = String("groupOp"); //简单的直接获取值
13 archFilter = new SearchFilter(); //对SearchFilter对象进⾏组装
14 archFilter.tGroupOp(groupOp);
15 archFilter.tRules(rules);
16 } catch (Exception e) {
17 System.out.println("filters=" + String() + ".json转换成实体类出错");
18 e.printStackTrace();
19 }
20 return archFilter;
21 }
这⾥只进⾏了SearchFilter类中list<SearchRule>rule属性的对象转换,因为它是⼀个SearchRule对象数组,需要单独拿出来进⾏json转换成对象,如果直接对filter进⾏ SearchFilter进⾏实体转换会报错。如果对象中不含有list,array,map等集合,可以像8-9⾏⼀样进⾏对象转换。
测试:
1 public static void main(String[] args) {
2 UrMgmtController con = new UrMgmtController();
3 String filters="{\"groupOp\": \"OR\",\"rules\": [{\"field\": \"realname\",\"op\": \"eq\","+
4 "\"data\": \"1234\"},{\"field\": \"cityCode\",\"op\": \"ne\",\"data\": \"5678\"}]}";路障僵尸
suffix5 SearchFilter archFilter =con.jsonToSearchFilter(filters);
6 System.out.println("gop="+GroupOp()+" rules.size="+Rules().size());
7 }
结果:gop=OR rules.size=2
将java对象转换成json格式:
1 //将Object 对象转换成 json
2 public String objectToJson(Object obj) {
3 ObjectMapper mapper = new ObjectMapper();
monte carlo4 // Convert object to JSON string
休斯顿大学怎么样5 String jsonStr = null;
6 try {
7 jsonStr = mapper.writeValueAsString(obj);
8 } catch (IOException e) {
9 (obj + "entity 轉換成 json出錯", e);
10 e.printStackTrace();
11 System.out.println("entity 轉換成 json出錯");
uge
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.PascalCaStrategy pp = new PropertyNamingStrategy.PascalCaStrategy();
6 mapper.tPropertyNamingStrategy(pp);
7 json = mapper.writeValueAsString(obj);
8 } catch (JsonProcessingException e) {
9 ("json对象转换失败",e);
10 e.printStackTrace();
11 }
12 return json;
13 }
结果:
"UpLoadDateTime":"2017-09-25 10:51:41","OrganizationId":"B3EA5A4D-7C60-4C21-AF09-494890467C34" ,"Test_Code":""别对我撒谎第一季