JAVA中json轉Map,jsonArray轉List集合,List集合轉json
阿新 • • 發佈:2021-08-25
//json字串轉換為MAP public static Map jsonStrToMap(String s) { Map map = new HashMap(); //注意這裡JSONObject引入的是net.sf.json net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(s); Iterator keys = json.keys(); while (keys.hasNext()) { String key= (String) keys.next(); String value = json.get(key).toString(); if (value.startsWith("{") && value.endsWith("}")) { map.put(key, jsonStrToMap(value)); } else { map.put(key, value); } }return map; } // 將jsonArray字串轉換成List集合 public static List jsonToList(String json, Class beanClass) { if (!StringUtils.isBlank(json)) { //這裡的JSONObject引入的是 com.alibaba.fastjson.JSONObject; return JSONObject.parseArray(json, beanClass); }else { return null; } } //List集合轉換為json public static JSON listToJson(List list) { JSON json=(JSON) JSON.toJSON(list); return json; }