在做数据接收的时候经常会遇到json转map或者json数组转listMap
String attachaudit = (String) param.get("attachaudit");
JSONArray objects = JSONObject.parseArray(attachaudit);
List<Map<String, Object>> auditListMap = (List<Map<String, Object>>) translate(objects);
/**
* 将JSONObjec对象转换成Map集合
*
* @param json
* @return
* @see
*/
public static HashMap<String, Object> translate(JSONObject json) {
HashMap<String, Object> map = new HashMap<String, Object>();
Set keys = json.keySet();
for (Object key : keys) {
Object o = json.get(key);
if (o instanceof JSONArray)
map.put((String) key, translate((JSONArray) o));
else if (o instanceof JSONObject)
map.put((String) key, translate((JSONObject) o));
else
map.put((String) key, o);
}
return map;
}
/**
* 将JSONArray对象转换成List集合
*
* @param json
* @return
* @see
*/
public static Object translate(JSONArray json) {
List<Object> list = new ArrayList<Object>();
for (Object o : json) {
if (o instanceof JSONArray)
list.add(translate((JSONArray) o));
else if (o instanceof JSONObject)
list.add(translate((JSONObject) o));
else
list.add(o);
}
return list;
}