json字符串转Map
1 import java.lang.reflect.InvocationTargetException; 2 import java.util.Date; 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import org.apache.commons.beanutils.BeanUtils; 7 import org.apache.commons.beanutils.ConvertUtils; 8 import org.apache.commons.beanutils.converters.BooleanConverter; 9 import org.apache.commons.beanutils.converters.DateConverter; 10 import org.apache.commons.beanutils.converters.DoubleConverter; 11 import org.apache.commons.beanutils.converters.IntegerConverter; 12 import org.apache.commons.beanutils.converters.LongConverter; 13 14 import com.iwhalecloud.retail.common.consts.CommonBaseMessageCodeEnum; 15 import com.iwhalecloud.retail.common.exception.BaseException; 16 import com.rabbitmq.tools.json.JSONReader; 17 import com.rabbitmq.tools.json.JSONWriter; 18 19 public abstract class JsonUtil { 20 21 private static JSONWriter jsonWriter = new JSONWriter(); 22 private static JSONReader jsonReader = new JSONReader(); 23 24 public static String object2Json(Object object) { 25 return jsonWriter.write(object); 26 } 27 28 /** 29 * json字符串转Object对象 30 * @param json 31 * @return 32 */ 33 public static Object json2Object(String json) { 34 return jsonReader.read(json); 35 } 36 37 private JsonUtil() { 38 39 } 40 41 public static <T> T json2T(String json, Class<T> clazz) throws InvocationTargetException, IllegalAccessException, InstantiationException, BaseException { 42 AssertUtil.isNotEmpty(json, CommonBaseMessageCodeEnum.REQ_PARAM_NOT_NULL); 43 Map<String, Object> map = (HashMap<String, Object>) JsonUtil.json2Object(json); 44 T t = clazz.newInstance(); 45 ConvertUtils.register(new DateConverter(), Date.class); 46 ConvertUtils.register(new LongConverter(null), Long.class); 47 ConvertUtils.register(new BooleanConverter(null), Boolean.class); 48 ConvertUtils.register(new IntegerConverter(null), Integer.class); 49 ConvertUtils.register(new DoubleConverter(null), Double.class); 50 BeanUtils.populate(t, map); 51 return t; 52 } 53 54 }
使用方式:
Map<String, Object> map = (HashMap<String, Object>) JsonUtil.json2Object(jsonStr);
1 import java.util.HashMap; 2 import java.util.Iterator; 3 import java.util.List; 4 import java.util.Map; 5 6 7 import org.apache.commons.lang.StringUtils; 8 import org.zgr.pack.entity.test.TestJsonToList; 9 10 import com.alibaba.fastjson.JSON; 11 import com.alibaba.fastjson.JSONObject; 12 13 14 public class Util { 15 //json字符串转换为MAP 16 public static Map jsonStrToMap(String s) { 17 Map map = new HashMap(); 18 //注意这里JSONObject引入的是net.sf.json 19 net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(s); 20 Iterator keys = json.keys(); 21 while (keys.hasNext()) { 22 String key = (String) keys.next(); 23 String value = json.get(key).toString(); 24 if (value.startsWith("{") && value.endsWith("}")) { 25 map.put(key, jsonStrToMap(value)); 26 } else { 27 map.put(key, value); 28 } 29 30 } 31 return map; 32 } 33 34 // 将jsonArray字符串转换成List集合 35 public static List jsonToList(String json, Class beanClass) { 36 if (!StringUtils.isBlank(json)) { 37 //这里的JSONObject引入的是 com.alibaba.fastjson.JSONObject; 38 return JSONObject.parseArray(json, beanClass); 39 } else { 40 return null; 41 } 42 } 43 44 //List集合转换为json 45 public static JSON listToJson(List list) { 46 JSON json=(JSON) JSON.toJSON(list); 47 return json; 48 } 49 50 51 public static void main(String[] args) { 52 53 System.out.println("---------------------json字符串转换为MAP---------------------"); 54 JSONObject jsonObject=new JSONObject(); 55 jsonObject.put("abc", 123); 56 jsonObject.put("def", 456); 57 System.out.println("A==========json====="+jsonObject); 58 Map map=Util.jsonStrToMap(jsonObject.toString()); 59 System.out.println("B==========def======"+map.get("def")); 60 61 62 System.out.println("---------------------将jsonArray字符串转换成List集合---------------------"); 63 String str="[{\"year\":\"2015\",\"month\":10,\"count\":47},{\"year\":2017,\"month\":12,\"count\":4}]"; 64 //这里需要指定泛型,我们建立一个实体类TestJsonToList 65 List<TestJsonToList> list=Util.jsonToList(str, TestJsonToList.class); 66 System.out.println("C==========取list第二个元素的year====="+list.get(1).getYear()); 67 68 69 System.out.println("---------------------将list集合转换成json---------------------"); 70 //这里的JSONObject引入的是 com.alibaba.fastjson.JSONObject; 71 JSON json=Util.listToJson(list); 72 System.out.println("D==========json====="+json); 73 } 74 }