首页 > 代码库 > 将JSON数据转换成Java的实体类

将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() { 7         return groupOp; 8     } 9 10     public void setGroupOp(String groupOp) {11         this.groupOp = groupOp;12     }13 14     public List<SearchRule> getRules() {15         return rules;16     }17 18     public void setRules(List<SearchRule> rules) {19         this.rules = rules;20     }
SearchRule 类:
 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 setField(String field) { 9         this.field = field;10     }11     public String getOp() {12         return op;13     }14     public void setOp(String op) {15         this.op = op;16     }17     public String getData() {18         return data;19     }20     public void setData(String data) {21         this.data =http://www.mamicode.com/ data;22     }23 24 25 }

 

解析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

 




将JSON数据转换成Java的实体类