json list map 简单的相互转换 /***alibaba.fastjson.jar* @param args */public void testJSON() { String strOne = "{one:1, two:2, three:3}"; //遍历JOSNObject JSONObject jsonObjectOne = JSONObject.parseObject(strOne); for(Entry json : j
/** *alibaba.fastjson.jar * @param args */ public void testJSON() { String strOne = "{one:1, two:2, three:3}"; //遍历JOSNObject JSONObject jsonObjectOne = JSONObject.parseObject(strOne); for(Entryjson : jsonObjectOne.entrySet()) { System.out.println(json.getKey()); System.out.println(json.getValue()); } String strTwo = "[1, 2, 3, 4]"; JSONArray jsonArrayOne = JSONArray.parseArray(strTwo); //遍历JSONArray for (Object object : jsonArrayOne) { System.out.println(object); } String strThree = "[5, 6, 7, 8]"; //以下字符串中有字母转化异常(具体原因不明) String strFore = "[5A1, 6B1, 7C1, 8D1]";//转化异常com.alibaba.fastjson.JSONException: syntax error, pos 3, json : [5A1, 6B1, 7C1, 8D1] String strFive = "[A1, B1, V1, D1]";//转化异常com.alibaba.fastjson.JSONException: syntax error, pos 2, json : [A1, B1, V1, D1] String strSix = "[a1, b1, 1c1, 2c1]";//转化异常com.alibaba.fastjson.JSONException: syntax error, pos 2, json : [a1, b1, 1c1, 2c1] String strSeven = "[abc, bbc, cbc, dbc]";//转化异常com.alibaba.fastjson.JSONException: syntax error, pos 2, json : [abc, bbc, cbc, dbc] //JSONArray to List List strList = JSONArray.parseArray(strThree, String.class); for (String string : strList) { System.out.println(string); } //List to JSONArray JSONArray array = JSONArray.parseArray(JSON.toJSONString(strList)); for (Object obj : array) { System.out.println(obj); } Map map = new HashMap (); map.put("one", 11); map.put("two", 22); //map to JSONObject JSONObject jsonObjectTwo = JSONObject.parseObject(JSON.toJSONString(map)); for(Entry json : jsonObjectTwo.entrySet()) { System.out.println(json.getKey()); System.out.println(json.getValue()); } //JSONObject to map Map mapTwo = JSONObject.parseObject(jsonObjectTwo.toString(), Map.class); for (Map.Entry entry : mapTwo.entrySet()) { System.out.println(entry.getKey()); System.out.println(entry.getValue()); } }