JSON工具类
package com.fh.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
*
* @author donald
*
*/
public class JsonUtil {
/**
* 对象转换成JSON字符串 obj
* @param obj 需要转换的对象
* @return 对象的string字符
*/
public static String toJson(Object obj) {
JSONObject jSONObject = JSONObject.fromObject(obj);
return jSONObject.toString();
}
/**
* JSON字符串转换成对象
* @param jsonString 需要转换的字符串
* @param type 需要转换的对象类型
* @return 对象
*/
@SuppressWarnings("unchecked")
public static <T> T fromJson(String jsonString, Class<T> type) {
JSONObject jsonObject = JSONObject.fromObject(jsonString);
return (T) JSONObject.toBean(jsonObject, type);
}
public static <T> String ListToJson(List<T> lobj){
JSONArray jsonArr = JSONArray.fromObject(lobj);
return jsonArr.toString();
}
/**
* 根据json String 返回List
* @param json
* @return
*/
public static List<Object> jsonToList(String json){
JSONArray jsonArr = JSONArray.fromObject(json);
return jsonToList(jsonArr);
}
/**
* 将Json String转化为 bean list
* @param jsonArray
* @param clazz
* @return
*/
@SuppressWarnings({ "rawtypes" })
public static List jsonToList(String json,Class clazz){
JSONArray jsonArr = JSONArray.fromObject(json);
return jsonToList(jsonArr,clazz);
}
/**
* 将JSONArray转化为 bean list
* @param jsonArray
* @param clazz
* @return
*/
@SuppressWarnings({ "rawtypes", "deprecation" })
public static List jsonToList( JSONArray jsonArray,Class clazz){
if(jsonArray!=null){
try{
List list = JSONArray.toList(jsonArray,clazz);
return list;
}catch(Exception e){
return null;
}
}else{
return null;
}
}
/**
* 将JSONArray对象转换成list集合 map
* @param jsonArr
* @return
*/
public static List<Object> jsonToList(JSONArray jsonArr) {
List<Object> list = new ArrayList<Object>();
for (Object obj : jsonArr) {
if (obj instanceof JSONArray) {
list.add(jsonToList((JSONArray) obj));
} else if (obj instanceof JSONObject) {
list.add(jsonToMap((JSONObject) obj));
} else {
list.add(obj);
}
}
return list;
}
/**
* 将json字符串转换成map对象
*
* @param json
* @return
*/
public static Map<String, Object> jsonToMap(String json) {
JSONObject obj = JSONObject.fromObject(json);
return jsonToMap(obj);
}
/**
* 将JSONObject转换成map对象
*
* @param json
* @return
*/
public static Map<String, Object> jsonToMap(JSONObject obj) {
Set<?> set = obj.keySet();
Map<String, Object> map = new HashMap<String, Object>(set.size());
for (Object key : obj.keySet()) {
Object value = obj.get(key);
if (value instanceof JSONArray) {
map.put(key.toString(), jsonToList((JSONArray) value));
} else if (value instanceof JSONObject) {
map.put(key.toString(), jsonToMap((JSONObject) value));
} else {
map.put(key.toString(), obj.get(key));
}
}
return map;
}
}
测试类
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import entity.Cat;
import util.JsonUtil;
public class testJson {
public static void main(String[] args){
Map<String ,String > bmap = new HashMap<String,String>();
bmap.put("user", "donald");
bmap.put("age", "32");
String bString = JsonUtil.toJson(bmap);
System.out.println("============map:"+bString);
Map<String, Object> bmaps = JsonUtil.jsonToMap(bString);
System.out.println("============user:"+bmaps.get("user"));
System.out.println("============age:"+bmaps.get("age"));
Cat cat1 = new Cat(4,"mini");
String cString = JsonUtil.toJson(cat1);
System.out.println("============Cat:"+cString);
Cat cat2 = JsonUtil.fromJson(cString, Cat.class);
System.out.println("============legs:"+cat2.getLegs());
System.out.println("============name:"+cat2.getName());
cat2.setLegs(5);
cat2.setName("big");
List<Cat> catList = new ArrayList<Cat>();
catList.add(cat1);
catList.add(cat2);
String clString = JsonUtil.ListToJson(catList);
System.out.println("============CatList:"+clString);
List<Cat> catListx= JsonUtil.jsonToList(clString,Cat.class);
Cat cat3 = catListx.get(0);
System.out.println("============legs:"+cat3.getLegs());
System.out.println("============name:"+cat3.getName());
}
}
结果输出:
============map:{"age":"32","user":"donald"}
============user:donald
============age:32
============Cat:{"legs":4,"name":"mini"}
============legs:4
============name:mini
============CatList:[{"legs":4,"name":"mini"},{"legs":5,"name":"big"}]
============legs:4
============name:mini