• 00025-工具类CommonJsonUtils:json 与 bean的转换


    package com.xmdishi.fmp.utils;
    
    import net.sf.json.util.JSONUtils;
    import org.apache.commons.lang3.StringUtils;
    
    import java.util.*;
    
    /**
     * json 与 bean的转换
     * chenjianquan
     * 2019/1/14
     */
    public class CommonJsonUtils<T> {
    
        /**
         * json转为List<T>
         * @author cjianquan
         * @date 2020/1/19
         * @param [json, clazz]
         * @return java.util.List<T>
         */
        public List<T> toBeanList(String json,Class<T> clazz){
            //调用DateMorpherEx,defaultValue为null
            JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpherEx(new String[] { "yyyy-MM-dd HH:mm:ss","yyyy-MM-dd" }, (Date) null));
            net.sf.json.JSONArray jsonArr = net.sf.json.JSONArray.fromObject(json);
            List<T> list = new ArrayList<T>();
            for (int i = 0; i < jsonArr.size(); i++) {
                Object obj = jsonArr.get(i);
                T rec = (T)net.sf.json.JSONObject.toBean(net.sf.json.JSONObject.fromObject(obj),clazz);
                list.add(rec);
            }
            return list;
        }
    
        /**
         * json 里包含这 key 的子数组,转换为List<T>
         * @author cjianquan
         * @date 2020/1/19
         * @param [json, key, clazz]
         * @return java.util.List<T>
         */
        public List<T> toBeanListByKey(String json,String key,Class<T> clazz){
            //调用DateMorpherEx,defaultValue为null
            JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpherEx(new String[] { "yyyy-MM-dd HH:mm:ss","yyyy-MM-dd" }, (Date) null));
            List<Map<String,Object>> mapList = getMapListByKey(json,key);
            net.sf.json.JSONArray jsonArr = net.sf.json.JSONArray.fromObject(mapList);
            List<T> list = new ArrayList<T>();
            for (int i = 0; i < jsonArr.size(); i++) {
                Object obj = jsonArr.get(i);
                T rec = (T)net.sf.json.JSONObject.toBean(net.sf.json.JSONObject.fromObject(obj),clazz);
                list.add(rec);
            }
            return list;
        }
    
        /**
         * 将json转为List<Map<String,Object>>
         * @author cjianquan
         * @date 2020/1/19
         * @param [json]
         * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
         */
        public List<Map<String,Object>> toMapList(String json){
            net.sf.json.JSONArray jsonArr = net.sf.json.JSONArray.fromObject(json);
            List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
            for (int i = 0; i < jsonArr.size(); i++) {
                net.sf.json.JSONObject obj = net.sf.json.JSONObject.fromObject(jsonArr.get(i));
                Iterator iterable= obj.keys();
                Map<String,Object> map = new HashMap<String,Object>();
                while(iterable.hasNext()){
                    Object key = iterable.next();
                    map.put(""+key,obj.get(key));
                }
                list.add(map);
            }
            return list;
        }
    
        /**
         * json 里包含这 key 的子数组,转换为List<Map<String,Object>>
         * @author cjianquan
         * @date 2020/1/19
         * @param [json, key]
         * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
         */
        public List<Map<String,Object>> getMapListByKey(String json,String key){
            List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
            net.sf.json.JSONObject params = net.sf.json.JSONObject.fromObject(json);
            Object objList = params.get(key);
            list = toMapList(objList.toString());
            return list;
        }
    
        /**
         * 获取json的 指定key 的 Map<String,Object>
         * @author cjianquan
         * @date 2020/1/19
         * @param [json, key]
         * @return java.util.Map<java.lang.String,java.lang.Object>
         */
        public Map<String,Object> getMapByKey(String json,String key){
            Map<String,Object> map = new HashMap<String,Object>();
            net.sf.json.JSONObject params = net.sf.json.JSONObject.fromObject(json);
            Object objMap = params.get(key);
            map = net.sf.json.JSONObject.fromObject(objMap);
            return map;
        }
    
    
        /**
         * 获取json的 指定key 的 Map<String,Object>,并转为 T
         * @author cjianquan
         * @date 2020/1/19
         * @param [json, key, clazz]
         * @return T
         */
        public T toBeanByKey(String json, String key, Class<T> clazz) {
            JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpherEx(new String[] { "yyyy-MM-dd HH:mm:ss","yyyy-MM-dd" }, (Date) null));
            net.sf.json.JSONArray jsonArr = net.sf.json.JSONArray.fromObject(json);
            Map<String,Object> map  = getMapByKey(json,key);
            T rec = (T)net.sf.json.JSONObject.toBean(net.sf.json.JSONObject.fromObject(map),clazz);
            return rec;
        }
    
        /**
         *  获取json的 指定key 的 json string
         * @author cjianquan
         * @date 2020/1/19
         * @param [json, key]
         * @return java.lang.String
         */
        public String getStringByKey(String json,String key){
            net.sf.json.JSONObject params = net.sf.json.JSONObject.fromObject(json);
            Object result = params.get(key);
            return ""+result;
        }
    
        public T toBean(String json, Class<T> clazz){
            JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpherEx(new String[] { "yyyy-MM-dd HH:mm:ss","yyyy-MM-dd" }, (Date) null));
            T rec = (T)net.sf.json.JSONObject.toBean(net.sf.json.JSONObject.fromObject(json),clazz);
            return rec;
        }
    
    }
    
    
  • 相关阅读:
    unity远程修改游戏配置
    object与byte[]的相互转换、文件与byte数组相互转换
    c#实现gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩
    Unity5.x在mac下的破解
    unity Socket TCP连接案例(一)
    Codeforces Edu Round 60 A-E
    Codeforces Edu Round 59 A-D
    Codeforces Edu Round 58 A-E
    Codeforces Edu Round 57 A-D
    Codeforces Edu Round 56 A-D
  • 原文地址:https://www.cnblogs.com/jianquan100/p/13667888.html
Copyright © 2020-2023  润新知