zoukankan      html  css  js  c++  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 = 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格式字符串的方法

    复制代码
     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 对象转换成 json
     2     public String objectToJson(Object obj) {
     3         ObjectMapper mapper = new ObjectMapper();
     4         // Convert object to JSON string
     5         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":"2017-09-25 10:51:41","OrganizationId":"B3EA5A4D-7C60-4C21-AF09-494890467C34" ,"Test_Code":""
  • 相关阅读:
    Docker容器Centos容器安装openssh
    DEVOPS技术实践_15:使用Docker作为Jenkins的slave
    DEVOPS技术实践_14:使用docker部署jenkins
    oracle数据库创建实例
    Java 覆写初探
    Java super和this小结
    Java 继承学习
    Java 数据表映射
    Java 单向链表学习
    Java 继承初探
  • 原文地址:https://www.cnblogs.com/smallfa/p/10495508.html
Copyright © 2011-2022 走看看