使用json-lib-*.jar的JSON解析工具类

使用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 }

 

posted @ 2016-08-17 10:41  SummerChill  阅读(372)  评论(0编辑  收藏  举报