Maven依赖:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;
import net.sf.json.util.PropertyFilter;
import java.util.*;
public class JSONUtil {
/////////////////////////////////////////////////Object2JSON///////////////////////////////////////////////////
public static JSONObject str2JSONObject(String str) {
return JSONObject.fromObject(str);
}
public static JSONArray javaBean2JSONArray(Object bean) {
return JSONArray.fromObject(bean);
}
public static JSONObject javaBean2JSONObject(Object bean) {
return JSONObject.fromObject(bean);
}
public static JSONArray list2JSONArray(List lst) {
JsonConfig config = new JsonConfig();
config.setJsonPropertyFilter(new PropertyFilter() {
public boolean apply(Object arg0, String arg1, Object arg2) {
if (arg1.equals("card") ||arg1.equals("fans")) {
return true;
} else {
return false;
}
}
});
return JSONArray.fromObject(lst);
}
public static JSONObject list2JSONObject(List<Object> lst) {
JSONObject jsonObj = new JSONObject();
jsonObj.put("objs", lst);
return jsonObj;
}
public static JSONObject map2JSONObject(Map map) {
return JSONObject.fromObject(map);
}
public static JSONArray map2JSONArray(Map map) {
return JSONArray.fromObject(map);
}
/////////////////////////////////////////////////JSON2Object///////////////////////////////////////////////////
public static Object jSONObject2javaBean(JSONObject jsonObject, Class cls) {
String[] dateFormats = new String[]{"yyyy-MM-dd"};
JSONUtils.getMorpherRegistry().registerMorpher(
new DateMorpher(dateFormats));
return JSONObject.toBean(jsonObject, cls);
}
public static List jSONArray2List(JSONArray jsArray) {
List list = null;
for (int i = 0; i < jsArray.size(); i++) {
JSONObject jsobj = jsArray.getJSONObject(i);
list.add(jsobj);
}
return list;
}
public static List<Map> jsonArray2MapList(Object obj) {
List<Map> lst = new ArrayList<Map>();
JSONArray jsArray = JSONArray.fromObject(obj);
Object[] array = jsArray.toArray();
for (int i = 0; i < array.length; i++) {
Map<String, Object> map = jSONObject2Map(array[i].toString());
lst.add(map);
}
return lst;
}
public static Map jSONObject2Map(Object obj) {
JSONObject js = JSONObject.fromObject(obj);
Iterator ite = js.keys();
Map<String, Object> map = new HashMap<String, Object>();
while (ite.hasNext()) {
Object o = ite.next();
Object oo = js.get(o.toString());
if (oo instanceof JSONObject) {
map.put(o.toString(), oo.toString());
} else if (oo instanceof JSONArray) {
JSONArray ooo = (JSONArray) oo;
List list = new ArrayList();
for (int i = 0; i < ooo.toArray().length; i++) { //for (int i = 0; i < ooo.length(); i++) {
String jso = ooo.getString(i);
list.add(jso.toString());
}
map.put(o.toString(), list);
} else {
map.put(o.toString(), oo);
}
}
return map;
}
public static Map<String, String> jSONObjectOfOneLevel2Map(JSONObject jsonObject) {
Map<String, String> map = new HashMap<String, String>();
Iterator it = jsonObject.keys();
// 遍历jsonObject数据,添加到Map对象
while (it.hasNext()) {
String key = String.valueOf(it.next());
String value = (String) jsonObject.get(key);
map.put(key, value);
}
return map;
}