使用json-lib-2.4-jdk15.jar
JSON工具类:
import java.util.List; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class JsonUtil { public static Object getObjectFromJsonString(String jsonString, Class<?> pojoCalss) { JSONObject jsonObject = JSONObject.fromObject(jsonString);
Object pojo = JSONObject.toBean(jsonObject, pojoCalss);
return pojo;
} public static String getJsonStringFromObject(Object javaObj) {
JSONObject json = JSONObject.fromObject(javaObj);
return json.toString();
} public static Object[] getObjectArrayFromJsonString(String jsonString) {
JSONArray jsonArray = JSONArray.fromObject(jsonString);
return jsonArray.toArray();
} public static String getJsonStringFromList(List<?> list) {
JSONArray jsonArray = JSONArray.fromObject(list);
return jsonArray.toString();
} }