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;
    }

}

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

上一篇:程序员必备的网站


下一篇:Mybatis逆向生成报错:.\mbg.xml (系统找不到指定的文件)