java创建JsonUtil工具类

package com.apexsoft.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JsonUtil {

    public static JSONObject optJSONObject(JSONObject pojo, String key) {
        if (pojo == null) {
            return new JSONObject();
        }
        String valueStr = pojo.getString(key);
        if (valueStr != null && valueStr.length() > 2) {
            try {
                return JSON.parseObject(valueStr);
            } catch (Exception e) {
                return JSON.parseObject(valueStr.substring(1, valueStr.length() - 1));
            }
        } else {
            return new JSONObject();
        }
    }

    public static JSONArray optJSONArray(JSONObject pojo, String key) {
        if (pojo == null) {
            return new JSONArray();
        }
        String valueStr = pojo.getString(key);
        if (valueStr != null && valueStr.length() > 2) {
            try {
                return JSON.parseArray(valueStr);
            } catch (Exception e) {
                return JSON.parseArray(valueStr.substring(1, valueStr.length() - 1));
            }
        } else {
            return new JSONArray();
        }
    }

    /**
     * 取对应key的字符串【当key不存在时,返回空串】
     *
     * @param pojo
     * @param key
     * @return
     */
    public static String optString(JSONObject pojo, String key) {
        return optString(pojo, key, "");
    }

    /**
     * 取对应key的字符串
     *
     * @param pojo
     * @param key
     * @param defaultValue
     * @return
     */
    public static String optString(JSONObject pojo, String key, String defaultValue) {
        if (pojo == null)
            return defaultValue;
        if (pojo.containsKey(key) && pojo.get(key) != null)
            return pojo.getString(key);
        else
            return defaultValue;
    }

    public static int optInt(JSONObject pojo, String key, int defaultValue) {
        if (pojo == null)
            return defaultValue;
        if (pojo.containsKey(key) && !pojo.getString(key).equals("") && pojo.get(key) != null)
            return pojo.getIntValue(key);
        else
            return defaultValue;
    }

    public static Object optObject(JSONObject pojo, String key, Object defaultValue) {
        if (pojo == null)
            return defaultValue;
        if (pojo.containsKey(key) && pojo.get(key) != null)
            return pojo.getObject(key, Object.class);
        else
            return defaultValue;
    }

    public static float optFloat(JSONObject pojo, String key, float defaultValue) {
        if (pojo == null)
            return defaultValue;
        if (pojo.containsKey(key) && !pojo.getString(key).equals("") && pojo.get(key) != null)
            return pojo.getFloatValue(key);
        else
            return defaultValue;
    }

    public static double optDouble(JSONObject pojo, String key, double defaultValue) {
        if (pojo == null)
            return defaultValue;
        if (pojo.containsKey(key) && !"".equals(pojo.getString(key)) && pojo.get(key) != null)
            return pojo.getDoubleValue(key);
        else
            return defaultValue;
    }

    public static boolean optBoolean(JSONObject pojo, String key, boolean defaultValue) {
        if (pojo == null)
            return defaultValue;
        if (pojo.containsKey(key) && pojo.get(key) != null)
            return pojo.getBooleanValue(key);
        else
            return defaultValue;
    }

    public static long optLong(JSONObject pojo, String key, long defaultValue) {
        if (pojo == null)
            return defaultValue;
        if (pojo.containsKey(key) && !"".equals(pojo.getString(key)) && pojo.get(key) != null)
            return pojo.getLongValue(key);
        else
            return defaultValue;
    }

    /**
     * json字符串转成map
     *
     * @param jsonStr
     * @return
     */
    public static Map<String, String> jsonstr2map(String jsonStr) {
        Map<String, String> map = new HashMap<String, String>();
        JSONObject json = JSON.parseObject(jsonStr);
        Iterator<String> it = json.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            Object value = json.get(key);
            if (value != null)
                map.put(key, value.toString());
            key = null;
            value = null;
        }
        it = null;
        json = null;
        return map;
    }

    /**
     * Map转换成json对象
     *
     * @param map
     * @return
     * @throws Exception
     */
    public static JSONObject map2json(Map<?, ?> map) {
        JSONObject result = new JSONObject();
        Object[] keys = map.keySet().toArray();
        for (int i = 0; i < keys.length; i++) {
            String key = (String) keys[i];
            Object value = map.get(key);
            if (value == null) {
                value = "";
            }
            result.put(key.toLowerCase(), value);
            key = null;
            value = null;
        }
        keys = null;
        return result;
    }

    /**
     * 根据KEY和VALUE新增JSON数组
     *
     * @param jsonarray_body
     * @return
     * @throws Exception
     */
    public static JSONArray addJsonArray(JSONArray jsonarray_body, int key, String value) {
        JSONObject json = new JSONObject();
        json.put("key", String.valueOf(key));
        json.put("value", value);
        json.put("zddm", "");
        json.put("mmzhlx", "");
        jsonarray_body.add(json);
        return jsonarray_body;
    }

    /**
     * 根据KEY和VALUE新增JSON数组
     *
     * @param jsonarray_body
     * @return
     * @throws Exception
     */
    public static JSONArray addJsonArray(JSONArray jsonarray_body, int key, String value, String zddm) {
        JSONObject json = new JSONObject();
        json.put("key", String.valueOf(key));
        json.put("value", value);
        json.put("zddm", zddm);
        json.put("mmzhlx", "");
        jsonarray_body.add(json);
        return jsonarray_body;
    }

    /**
     * 根据KEY和VALUE新增JSON数组
     *
     * @param jsonarray_body
     * @return
     * @throws Exception
     */
    public static JSONArray addJsonArray(JSONArray jsonarray_body, int key, String value, String zddm, int mmzhlx) {
        JSONObject json = new JSONObject();
        json.put("key", String.valueOf(key));
        json.put("value", value);
        json.put("zddm", zddm);
        json.put("mmzhlx", mmzhlx);
        jsonarray_body.add(json);
        return jsonarray_body;
    }

    /**
     * 根据KEY和VALUE新增JSON数组
     *
     * @param jsonarray_body
     * @return
     * @throws Exception
     */
    public static JSONArray addJsonArray(JSONArray jsonarray_body, String key, String value) {
        JSONObject json = new JSONObject();
        json.put("key", key);
        json.put("value", value);
        jsonarray_body.add(json);
        return jsonarray_body;
    }

    /**
     * 根据KEY和VALUE新增JSON数组
     *
     * @param
     * @return
     * @throws Exception
     */
    public static JSONArray addJsonArray(JSONArray jsonarray_body, String key, String value, String zddm) {
        JSONObject json = new JSONObject();
        json.put("key", String.valueOf(key));
        json.put("value", value);
        json.put("zddm", zddm);
        json.put("mmzhlx", "");
        jsonarray_body.add(json);
        return jsonarray_body;
    }

    public static JSONArray addJsonArray(JSONArray jsonarray_body, String key, String value, String zddm, int mmzhlx) {
        JSONObject json = new JSONObject();
        json.put("key", String.valueOf(key));
        json.put("value", value);
        json.put("zddm", zddm);
        json.put("mmzhlx", mmzhlx);
        jsonarray_body.add(json);
        return jsonarray_body;
    }

    /**
     * 获取JsonArray首个符合指定key|value的索引
     *
     * @param jsonarray_body 源jsonarray
     * @param key            目标key
     * @param value          目标value
     * @return index          无符合条件:-1,有符合条件:数组元素索引值
     */
    public static int getJArrayKeyValueIndex(JSONArray jsonarray_body, String key, String value) {
        if (jsonarray_body == null) {
            return -1;
        }
        JSONObject json = new JSONObject();
        for (int i = 0; i < jsonarray_body.size(); i++) {
            json = (JSONObject) jsonarray_body.get(i);
            if (json.containsKey(key) && optString(json, key, "").equals(value)) {
                return i;
            }
        }
        return -1;
    }


}

  

posted @ 2022-02-15 17:00  梦幻&浮云%  阅读(136)  评论(0)    收藏  举报