当前位置 : 主页 > 编程语言 > c++ >

json map list 之间转换的工具类

来源:互联网 收集:自由互联 发布时间:2021-06-30
工作中自己写或者收集的jason转map,list 或者list,map转json的工具集合 /** * Created with IntelliJ IDEA. * User: yuyue * Date: 14-7-23 * Time: 下午7:51 */public class JsonHelper { /** * map转json * * @param object 是一
工作中自己写或者收集的jason转map,list 或者list,map转json的工具集合
/**
 * Created with IntelliJ IDEA.
 * User: yuyue
 * Date: 14-7-23
 * Time: 下午7:51
 */
public class JsonHelper {

    /**
     * map转json
     *
     * @param object 是一个map
     * @return
     * @throws JSONException
     */
    public static Object mapToJSON(Object object) throws JSONException {
        if (object instanceof Map) {
            JSONObject json = new JSONObject();
            Map map = (Map) object;
            for (Object key : map.keySet()) {
                json.put(key.toString(), mapToJSON(map.get(key)));
            }
            return json;
        } else if (object instanceof Iterable) {

            JSONArray json = new JSONArray();

            for (Object value : ((Iterable) object)) {
                json.put(value);
            }

            return json;
        } else {
            return object;
        }
    }


    /**
     * list转json
     * @param object
     * @return
     */
    public static JSONArray listToJson(Iterable object){

            JSONArray json = new JSONArray();

            for (Object value : object) {
                json.put(value);
            }

            return json;
    }

    public static boolean isEmptyObject(JSONObject object) {
        return object.names() == null;
    }

    public static Map
 
   getMap(JSONObject object, String key) throws JSONException {
        return toMap(object.getJSONObject(key));
    }

    /**
     * json转map
     *
     * @param object
     * @return
     * @throws JSONException
     */
    public static HashMap
  
    toMap(JSONObject object) throws JSONException { HashMap
   
     map = new HashMap(); Iterator keys = object.keys(); while (keys.hasNext()) { String key = (String) keys.next(); map.put(key, fromJson(object.get(key))); } return map; } /** * json转list * * @param array * @return * @throws JSONException */ public static List toList(JSONArray array) throws JSONException { List list = new ArrayList(); for (int i = 0; i < array.length(); i++) { list.add(fromJson(array.get(i))); } return list; } /** * 根据情况将json转换为list 或者map * * @param json * @return * @throws JSONException */ private static Object fromJson(Object json) throws JSONException { if (json == JSONObject.NULL) { return null; } else if (json instanceof JSONObject) { return toMap((JSONObject) json); } else if (json instanceof JSONArray) { return toList((JSONArray) json); } else { return json; } } /** * 将String转换成json * * @param s * @return */ public static JSONObject stringToJson(String s) { JSONObject jsonObject = null; try { jsonObject = new JSONObject(s); } catch (JSONException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } return jsonObject; } /** * 将String转换成json * * @param s * @return */ public static JSONArray stringToJsonArray(String s) { JSONArray jsonObject = null; try { jsonObject = new JSONArray(s); } catch (JSONException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } return jsonObject; } }
   
  
 
网友评论