• json工具类


    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    
    import java.util.*;
    
    public class JSONUtil {
        public JSONUtil() {
        }
    
        public static <T> String parseObj2JSON(T obj) {
            if (obj == null) {
                return "";
            }
            JSONObject jsonObj = JSONObject.fromObject(obj);
            return jsonObj.toString();
        }
    
        public static <T> T parseJSON2Obj(String jsonStr, Class clazz) {
            JSONObject jsonObj = JSONObject.fromObject(jsonStr);
            return (T) JSONObject.toBean(jsonObj, clazz);
        }
    
        public static <T> String parseList2JSON(List<T> objList) {
            if (objList == null) {
                return "";
            }
            JSONArray jsonArr = JSONArray.fromObject(objList);
            return jsonArr.toString();
        }
    
        public static <T> List<T> parseJSON2List(String jsonStr, Class clazz) {
            JSONArray jsonArr = JSONArray.fromObject(jsonStr);
            return (List<T>) JSONArray.toCollection(jsonArr, clazz);
        }
    
        public static <T> String parseMap2JSON(Map<String, T> map) {
            if (map == null) {
                return "";
            }
            JSONObject jsonObj = JSONObject.fromObject(map);
            return jsonObj.toString();
        }
    
        public static Map<String, Object> parseJSON2Map(String jsonStr) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            JSONObject jsonObj = JSONObject.fromObject(jsonStr);
            Iterator var = jsonObj.keySet().iterator();
    
            while (var.hasNext()) {
                Object key = var.next();
                Object val = jsonObj.get(key);
                if (val instanceof JSONArray) {
                    ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                    Iterator it = ((JSONArray) val).iterator();
    
                    while (it.hasNext()) {
                        JSONObject json = (JSONObject) it.next();
                        list.add(parseJSON2Map(json.toString()));
                    }
    
                    map.put(key.toString(), list);
                } else {
                    map.put(key.toString(), val);
                }
            }
    
            return map;
        }
    
        public static <T> Map<String, T> parseJSON2Map(String jsonStr, Class clazz) {
            HashMap map = new HashMap();
            JSONObject jsonObj = JSONObject.fromObject(jsonStr);
            Iterator var = jsonObj.keySet().iterator();
            while (var.hasNext()) {
                Object key = var.next();
                Object val = jsonObj.get(key);
                if (val instanceof JSONArray) {
                    ArrayList list = new ArrayList();
                    Iterator it = ((JSONArray) val).iterator();
                    while (it.hasNext()) {
                        JSONObject json = (JSONObject) it.next();
                        list.add(parseJSON2Map(json.toString(), clazz));
                    }
                    map.put(key.toString(), list);
                } else if (val instanceof JSONObject) {
                    map.put(key.toString(), JSONObject.toBean((JSONObject) val, clazz));
                } else {
                    map.put(key.toString(), val);
                }
            }
    
            return map;
        }
    
        public static String parseMapList2JSON(List<Map<String, Object>> mapList) {
            if (mapList == null) {
                return "";
            }
            JSONArray jsonArr = JSONArray.fromObject(mapList);
            return jsonArr.toString();
        }
    
        public static List<Map<String, Object>> parseJSON2List(String jsonStr) {
            JSONArray jsonArr = JSONArray.fromObject(jsonStr);
            ArrayList<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
            Iterator it = jsonArr.iterator();
    
            while (it.hasNext()) {
                JSONObject json = (JSONObject) it.next();
                mapList.add(parseJSON2Map(json.toString()));
            }
            return mapList;
        }
    
        public static <T> T parseJSON2Obj(String jsonStr, Class clazz, Map<String, Class> classMap) {
            JSONObject jsonObj = JSONObject.fromObject(jsonStr);
            return (T) JSONObject.toBean(jsonObj, clazz, classMap);
        }
    
        public static String encode(String str){
            return str.replace(""", "\"");
        }
    }
  • 相关阅读:
    python 赋值操作的知识点
    python while循环语句
    python dict遍历
    python列表的切片操作
    Python做下载器需要掌握哪些
    BeautifulSoup已经安装,但仍提示No module named
    python 列表循环输出中文
    python 字符串split (string split)
    python 调用解释器
    分享python字符串去除空格的知识点
  • 原文地址:https://www.cnblogs.com/dongfanghao/p/8267017.html
Copyright © 2020-2023  润新知