map和json互转

/** * 转换MP自己定义的JSONArray对象为List对象 * @param obj * @return */ public static List jsonArrToList(JSONArray arr){ String key = null; String val = null; Iterator it = null; JSONObject obj = null; Map map = null; List list = new ArrayList(); try { for (int i = 0; i < arr.length(); i++) { map = new HashMap(); obj = arr.getJSONObject(i); it = obj.keys(); while(it.hasNext()){ key = it.next(); val =(obj.get(key)).toString(); map.put(key, val); } list.add(map); } } catch (JSONException e) { e.printStackTrace(); } return list; } /** * 转换MP自己定义的JSONArray对象为List对象 * @param obj * @return */ public static JSONArray listToJsonArr(List list){ String key = null; String val = null; Iterator it = null; JSONObject obj = null; Map map = null; JSONArray arr = new JSONArray(); try { for (int i = 0; i < list.size(); i++) { obj = new JSONObject(); map = list.get(i); it = map.keySet().iterator(); while(it.hasNext()){ key = it.next(); val =(map.get(key)).toString(); obj.put(key, val); } arr.put(obj); } } catch (JSONException e) { e.printStackTrace(); } return arr; }

【map和json互转】

    推荐阅读