public class ListOrMapForJson { public static List<Map<String, Object>> getlistForJson(String jsonStr) { List<Map<String, Object>> list = null; try { JSONArray jsonArray = new JSONArray(jsonStr); JSONObject jsonObj; list = new ArrayList<Map<String, Object>>(); for (int i = 0; i < jsonArray.length(); i++) { jsonObj = (JSONObject) jsonArray.get(i); list.add(getMapForJson(jsonObj.toString())); } } catch (Exception e) { e.printStackTrace(); } return list; } public static Map<String, Object> getMapForJson(String jsonStr) { JSONObject jsonObject; try { jsonObject = new JSONObject(jsonStr); Iterator<String> keyIter = jsonObject.keys(); String key; Object value; Map<String, Object> valueMap = new HashMap<String, Object>(); while (keyIter.hasNext()) { key = keyIter.next(); value = jsonObject.get(key); valueMap.put(key, value); } return valueMap; } catch (Exception e) { e.printStackTrace(); } return null; } }
json字符串转List/Map的工具类
最新推荐文章于 2024-08-05 10:42:39 发布