JSON解析总结2

使用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();
} }
上一篇:九天学会Java,第一天,变量和数据类型,赋值和输出


下一篇:javaweb作业一