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

2021-01-02 11:28

阅读:791

标签:def   array   auth   ++   HERE   tst   jlist   @param   gis   

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 {

    /**
     * json转为List
     * @author cjianquan
     * @date 2020/1/19
     * @param [json, clazz]
     * @return java.util.List
     */
    public List toBeanList(String json,Class 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 list = new ArrayList();
        for (int i = 0; i 
     * @author cjianquan
     * @date 2020/1/19
     * @param [json, key, clazz]
     * @return java.util.List
     */
    public List toBeanListByKey(String json,String key,Class clazz){
        //调用DateMorpherEx,defaultValue为null
        JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpherEx(new String[] { "yyyy-MM-dd HH:mm:ss","yyyy-MM-dd" }, (Date) null));
        List> mapList = getMapListByKey(json,key);
        net.sf.json.JSONArray jsonArr = net.sf.json.JSONArray.fromObject(mapList);
        List list = new ArrayList();
        for (int i = 0; i >
     * @author cjianquan
     * @date 2020/1/19
     * @param [json]
     * @return java.util.List>
     */
    public List> toMapList(String json){
        net.sf.json.JSONArray jsonArr = net.sf.json.JSONArray.fromObject(json);
        List> list = new ArrayList>();
        for (int i = 0; i  map = new HashMap();
            while(iterable.hasNext()){
                Object key = iterable.next();
                map.put(""+key,obj.get(key));
            }
            list.add(map);
        }
        return list;
    }

    /**
     * json 里包含这 key 的子数组,转换为List>
     * @author cjianquan
     * @date 2020/1/19
     * @param [json, key]
     * @return java.util.List>
     */
    public List> getMapListByKey(String json,String key){
        List> list = new ArrayList>();
        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
     * @author cjianquan
     * @date 2020/1/19
     * @param [json, key]
     * @return java.util.Map
     */
    public Map getMapByKey(String json,String key){
        Map map = new HashMap();
        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,并转为 T
     * @author cjianquan
     * @date 2020/1/19
     * @param [json, key, clazz]
     * @return T
     */
    public T toBeanByKey(String json, String key, Class 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 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 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的转换

标签:def   array   auth   ++   HERE   tst   jlist   @param   gis   

原文地址:https://www.cnblogs.com/jianquan100/p/13667888.html


评论


亲,登录后才可以留言!