json转List、Map
1 import java.util.ArrayList; 2 import java.util.HashMap; 3 import java.util.Iterator; 4 import java.util.List; 5 import java.util.Map; 6 import java.util.Map.Entry; 7 8 import com.alibaba.fastjson.JSONArray; 9 import com.alibaba.fastjson.JSONObject; 10 import com.alibaba.fastjson.serializer.SerializerFeature; 11 12 public class JsonUtil { 13 /** 14 * json字符串转化为数组 15 * @param json 16 * @return 17 */ 18 public static List<Map<String, Object>> json2List(Object json) { 19 JSONArray jsonArr = (JSONArray) json; 20 List<Map<String, Object>> arrList = new ArrayList<Map<String, Object>>(); 21 for (int i = 0; i < jsonArr.size(); ++i) { 22 arrList.add(strJson2Map(jsonArr.getString(i))); 23 } 24 return arrList; 25 } 26 27 /** 28 * json字符串转化为Map 29 * @param json 30 * @return 31 */ 32 public static Map<String, Object> strJson2Map(String json) { 33 JSONObject jsonObject = JSONObject.parseObject(json); 34 Map<String, Object> resMap = new HashMap<String, Object>(); 35 Iterator<Entry<String, Object>> it = jsonObject.entrySet().iterator(); 36 while (it.hasNext()) { 37 Map.Entry<String, Object> param = (Map.Entry<String, Object>) it.next(); 38 if (param.getValue() instanceof JSONObject) { 39 resMap.put(param.getKey(), strJson2Map(param.getValue().toString())); 40 } else if (param.getValue() instanceof JSONArray) { 41 resMap.put(param.getKey(), json2List(param.getValue())); 42 } else { 43 resMap.put(param.getKey(), JSONObject.toJSONString(param.getValue(), SerializerFeature.WriteClassName)); 44 } 45 } 46 return resMap; 47 } 48 }
json、List互转
1 package com.listandjson; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import net.sf.json.JSONArray; 6 7 public class Test { 8 9 public static void main(String[] args) { 10 List<String> list = new ArrayList<String>(); 11 list.add("abc"); 12 list.add("123"); 13 14 //list转成json 15 String json =JSONArray.fromObject(list).toString(); 16 System.out.println(json); 17 //运行:["abc","123"] 18 19 //json转成list 20 JSONArray jsonArray = JSONArray.fromObject(json); 21 List<String> list2 = (List) JSONArray.toCollection(jsonArray); 22 for (int i = 0; i < list2.size(); i++) { 23 System.out.println(list2.get(i)); 24 //运行:abc 25 // 123 26 } 27 } 28 }
jsonStr转Map
1 package com.json; 2 3 import com.alibaba.fastjson.JSON; 4 import com.alibaba.fastjson.JSONObject; 5 import java.util.Map; 6 7 /** 8 * JSON字符串自动转换 9 * Created by zkn on 2016/8/22. 10 */ 11 public class JsonToMapTest01 { 12 13 public static void main(String[] args){ 14 15 String str = "{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}"; 16 //第一种方式 17 Map maps = (Map)JSON.parse(str); 18 System.out.println("这个是用JSON类来解析JSON字符串!!!"); 19 for (Object map : maps.entrySet()){ 20 System.out.println(((Map.Entry)map).getKey()+" " + ((Map.Entry)map).getValue()); 21 } 22 //第二种方式 23 Map mapTypes = JSON.parseObject(str); 24 System.out.println("这个是用JSON类的parseObject来解析JSON字符串!!!"); 25 for (Object obj : mapTypes.keySet()){ 26 System.out.println("key为:"+obj+"值为:"+mapTypes.get(obj)); 27 } 28 //第三种方式 29 Map mapType = JSON.parseObject(str,Map.class); 30 System.out.println("这个是用JSON类,指定解析类型,来解析JSON字符串!!!"); 31 for (Object obj : mapType.keySet()){ 32 System.out.println("key为:"+obj+"值为:"+mapType.get(obj)); 33 } 34 //第四种方式 35 /** 36 * JSONObject是Map接口的一个实现类 37 */ 38 Map json = (Map) JSONObject.parse(str); 39 System.out.println("这个是用JSONObject类的parse方法来解析JSON字符串!!!"); 40 for (Object map : json.entrySet()){ 41 System.out.println(((Map.Entry)map).getKey()+" "+((Map.Entry)map).getValue()); 42 } 43 //第五种方式 44 /** 45 * JSONObject是Map接口的一个实现类 46 */ 47 JSONObject jsonObject = JSONObject.parseObject(str); 48 System.out.println("这个是用JSONObject的parseObject方法来解析JSON字符串!!!"); 49 for (Object map : json.entrySet()){ 50 System.out.println(((Map.Entry)map).getKey()+" "+((Map.Entry)map).getValue()); 51 } 52 //第六种方式 53 /** 54 * JSONObject是Map接口的一个实现类 55 */ 56 Map mapObj = JSONObject.parseObject(str,Map.class); 57 System.out.println("这个是用JSONObject的parseObject方法并执行返回类型来解析JSON字符串!!!"); 58 for (Object map: json.entrySet()){ 59 System.out.println(((Map.Entry)map).getKey()+" "+((Map.Entry)map).getValue()); 60 } 61 String strArr = "{{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}," + 62 "{\"00\":\"zhangsan\",\"11\":\"lisi\",\"22\":\"wangwu\",\"33\":\"maliu\"}}"; 63 // JSONArray.parse() 64 System.out.println(json); 65 } 66 }