对象POJO和JSON互转
public class JsonUtil {
/**
* JSON 转 POJO
*/
public static <T> T getObject(String pojo, Class<T> tclass) {
try {
return JSONObject.parseObject(pojo, tclass);
} catch (Exception e) {
log.error(tclass + "转 JSON 失败");
}
return null;
}
/**
* POJO 转 JSON
*/
public static <T> String getJson(T tResponse){
String pojo = JSONObject.toJSONString(tResponse);
return pojo;
}
}
List集合和JSON互转工具类
public class JsonListUtil {
/**
* List<T> 转 json 保存到数据库
*/
public static <T> String listToJson(List<T> ts) {
String jsons = JSON.toJSONString(ts);
return jsons;
}
/**
* json 转 List<T>
*/
public static <T> List<T> jsonToList(String jsonString, Class<T> clazz) {
@SuppressWarnings("unchecked")
List<T> ts = (List<T>) JSONArray.parseArray(jsonString, clazz);
return ts;
}
}
- public class JsonToMapTest01 {
-
- public static void main(String[] args){
-
- String str = "{"0":"zhangsan","1":"lisi","2":"wangwu","3":"maliu"}";
-
- Map maps = (Map)JSON.parse(str);
- System.out.println("这个是用JSON类来解析JSON字符串!!!");
- for (Object map : maps.entrySet()){
- System.out.println(((Map.Entry)map).getKey()+" " + ((Map.Entry)map).getValue());
- }
-
- Map mapTypes = JSON.parseObject(str);
- System.out.println("这个是用JSON类的parseObject来解析JSON字符串!!!");
- for (Object obj : mapTypes.keySet()){
- System.out.println("key为:"+obj+"值为:"+mapTypes.get(obj));
- }
-
- Map mapType = JSON.parseObject(str,Map.class);
- System.out.println("这个是用JSON类,指定解析类型,来解析JSON字符串!!!");
- for (Object obj : mapType.keySet()){
- System.out.println("key为:"+obj+"值为:"+mapType.get(obj));
- }
-
-
- Map json = (Map) JSONObject.parse(str);
- System.out.println("这个是用JSONObject类的parse方法来解析JSON字符串!!!");
- for (Object map : json.entrySet()){
- System.out.println(((Map.Entry)map).getKey()+" "+((Map.Entry)map).getValue());
- }
-
-
- JSONObject jsonObject = JSONObject.parseObject(str);
- System.out.println("这个是用JSONObject的parseObject方法来解析JSON字符串!!!");
- for (Object map : json.entrySet()){
- System.out.println(((Map.Entry)map).getKey()+" "+((Map.Entry)map).getValue());
- }
-
-
- Map mapObj = JSONObject.parseObject(str,Map.class);
- System.out.println("这个是用JSONObject的parseObject方法并执行返回类型来解析JSON字符串!!!");
- for (Object map: json.entrySet()){
- System.out.println(((Map.Entry)map).getKey()+" "+((Map.Entry)map).getValue());
- }
- String strArr = "{{"0":"zhangsan","1":"lisi","2":"wangwu","3":"maliu"}," +
- "{"00":"zhangsan","11":"lisi","22":"wangwu","33":"maliu"}}";
-
- System.out.println(json);
- }
- }