package com.shscn;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * JSON字符串生成类
 * @author qm 
 */
public class JsonUtil {
	/** 实体类转换为JSON */
	public static String toJson(Object obj){
		if(obj == null) {
			return "{}";
		}
		JSONObject jo = new JSONObject(obj);
		return jo.toString();
	}
	
	/** list转换为JSON */
	public static String toJson(Collection<?> coll) {
		if(coll == null) {
			return "[]";
		}
		JSONArray ja = new JSONArray(coll);
		return ja.toString();
	}
	
	/** map转换为JSON */
	public static <K, V> String toJson(Map<K, V> map) {
		if(map == null) {
			return "{}";
		}
		List<Map<K, V>> list = new ArrayList<Map<K, V>>();
		list.add(map);
		String str = JsonUtil.toJson(list);
		return str.substring(1, str.length()-1);
	}
	
	/**
	 * JSON转换为map
	 * @param json
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <K, V> Map<K, V> toMap(String json) {
		return new JSONObject(json).getMap();
	}
	
	 /**
     * 将json字符串转换为List集合
     * @param jsonArrStr
     * @return
     */
    @SuppressWarnings("unchecked")
	public static List<Map<String, Object>> jsonObjList(String jsonArrStr) {
        List<Map<String, Object>> jsonList = new ArrayList<Map<String, Object>>();
        JSONArray jsonArr = null;
        try {
            jsonArr = new JSONArray(jsonArrStr);
            jsonList = (List<Map<String, Object>>)JsonUtil.jsonToList(jsonArr);
        } catch (JSONException e) {
            System.out.println("Json字符串转换异常!");
            e.printStackTrace();
        }
        return jsonList;
    }
    
    /**
     * 将传递近来的json数组转换为List集合
     * @param jsonArr
     * @return
     * @throws JSONException
     */
    private static List<?> jsonToList(JSONArray jsonArr)
            throws JSONException {
        List<Object> jsonToMapList = new ArrayList<Object>();
        for (int i = 0; i < jsonArr.length(); i++) {
            Object object = jsonArr.get(i);
            if (object instanceof JSONArray) {
                jsonToMapList.add(JsonUtil.jsonToList((JSONArray) object));
            } else if (object instanceof JSONObject) {
                jsonToMapList.add(JsonUtil.jsonToMap((JSONObject) object));
            } else {
                jsonToMapList.add(object);
            }
        }
        return jsonToMapList;
    }
    
    /**
     * 将传递近来的json对象转换为Map集合
     * @param jsonObj
     * @return
     * @throws JSONException
     */
    @SuppressWarnings("unchecked")
    private static Map<String, Object> jsonToMap(JSONObject jsonObj)
            throws JSONException {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        Iterator<String> jsonKeys = jsonObj.keys();
        while (jsonKeys.hasNext()) {
            String jsonKey = jsonKeys.next();
            Object jsonValObj = jsonObj.get(jsonKey);
            if (jsonValObj instanceof JSONArray) {
                jsonMap.put(jsonKey, JsonUtil.jsonToList((JSONArray) jsonValObj));
            } else if (jsonValObj instanceof JSONObject) {
                jsonMap.put(jsonKey, JsonUtil.jsonToMap((JSONObject) jsonValObj));
            } else {
                jsonMap.put(jsonKey, jsonValObj);
            }
        }
        return jsonMap;
    }
	
}
最近下载更多
75431368  LV10 2021年7月16日
马儿爱吃兰  LV10 2021年6月15日
wangshixi2010  LV3 2020年8月21日
wjschm  LV14 2020年4月27日
huaua7676  LV30 2020年1月15日
benyan  LV8 2019年4月29日
阿毛123456  LV13 2019年4月14日
dmzhjg  LV6 2018年8月31日
shiwenliang  LV5 2018年5月30日
byj1987  LV18 2018年5月13日
最近浏览更多
666ing  LV2 2023年9月27日
flyjoe  LV3 2023年7月11日
ericxu1116  LV24 2023年6月26日
2292250314  LV2 2023年5月28日
王培龚  LV4 2023年4月10日
hkxyyz  LV6 2022年11月14日
lilu0226  LV7 2022年7月31日
帅潇潇  LV12 2022年6月13日
Trickster  LV9 2022年5月17日
crosa_Don  LV18 2022年4月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友