使用json-lib-2.4-jdk15.jar
JSON工具类:
1 import java.util.List; 2 3 import net.sf.json.JSONArray; 4 import net.sf.json.JSONObject; 5 6 public class JsonUtil { 7 8 public static Object getObjectFromJsonString(String jsonString, Class<?> pojoCalss) { 9 10 JSONObject jsonObject = JSONObject.fromObject(jsonString); 11 Object pojo = JSONObject.toBean(jsonObject, pojoCalss); 12 return pojo; 13 } 14 15 public static String getJsonStringFromObject(Object javaObj) { 16 JSONObject json = JSONObject.fromObject(javaObj); 17 return json.toString(); 18 } 19 20 public static Object[] getObjectArrayFromJsonString(String jsonString) { 21 JSONArray jsonArray = JSONArray.fromObject(jsonString); 22 return jsonArray.toArray(); 23 } 24 25 public static String getJsonStringFromList(List<?> list) { 26 JSONArray jsonArray = JSONArray.fromObject(list); 27 return jsonArray.toString(); 28 } 29 30 }
本文转自SummerChill博客园博客,原文链接:http://www.cnblogs.com/DreamDrive/p/5779007.html,如需转载请自行联系原作者