java中使用net.sf.json对json进行解析

net.sf.json依赖的包很多。

有commons-collections,commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph-1.0.5.jar,morph-1.1.1.jar

  1. /**
  2.      * 从一个JSON 对象字符格式中得到一个java对象,形如:
  3.      * {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}}
  4.      * @param object
  5.      * @param clazz
  6.      * @return
  7.      */
  8. public static Object getDTO(String jsonString, Class clazz){   
  9.         JSONObject jsonObject = null;   
  10. try{   
  11.             setDataFormat2JAVA();    
  12.             jsonObject = JSONObject.fromObject(jsonString);   
  13.         }catch(Exception e){   
  14.             e.printStackTrace();   
  15.         }   
  16. return JSONObject.toBean(jsonObject, clazz);   
  17.     }   
  18. /**
  19.      * 从一个JSON 对象字符格式中得到一个java对象,其中beansList是一类的集合,形如:
  20.      * {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...},
  21.      * beansList:[{}, {}, ...]}
  22.      * @param jsonString
  23.      * @param clazz
  24.      * @param map 集合属性的类型 (key : 集合属性名, value : 集合属性类型class) eg: ("beansList" : Bean.class)
  25.      * @return
  26.      */
  27. public static Object getDTO(String jsonString, Class clazz, Map map){   
  28.         JSONObject jsonObject = null;   
  29. try{   
  30.             setDataFormat2JAVA();    
  31.             jsonObject = JSONObject.fromObject(jsonString);   
  32.         }catch(Exception e){   
  33.             e.printStackTrace();   
  34.         }   
  35. return JSONObject.toBean(jsonObject, clazz, map);   
  36.     }   
  37. /**
  38.      * 从一个JSON数组得到一个java对象数组,形如:
  39.      * [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...]
  40.      * @param object
  41.      * @param clazz
  42.      * @return
  43.      */
  44. public static Object[] getDTOArray(String jsonString, Class clazz){   
  45.         setDataFormat2JAVA();   
  46.         JSONArray array = JSONArray.fromObject(jsonString);   
  47.         Object[] obj = new Object[array.size()];   
  48. for(int i = 0; i < array.size(); i++){   
  49.             JSONObject jsonObject = array.getJSONObject(i);   
  50.             obj[i] = JSONObject.toBean(jsonObject, clazz);   
  51.         }   
  52. return obj;   
  53.     }   
  54. /**
  55.      * 从一个JSON数组得到一个java对象数组,形如:
  56.      * [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...]
  57.      * @param object
  58.      * @param clazz
  59.      * @param map
  60.      * @return
  61.      */
  62. public static Object[] getDTOArray(String jsonString, Class clazz, Map map){   
  63.         setDataFormat2JAVA();   
  64.         JSONArray array = JSONArray.fromObject(jsonString);   
  65.         Object[] obj = new Object[array.size()];   
  66. for(int i = 0; i < array.size(); i++){   
  67.             JSONObject jsonObject = array.getJSONObject(i);   
  68.             obj[i] = JSONObject.toBean(jsonObject, clazz, map);   
  69.         }   
  70. return obj;   
  71.     }   
  72. /**
  73.      * 从一个JSON数组得到一个java对象集合
  74.      * @param object
  75.      * @param clazz
  76.      * @return
  77.      */
  78. public static List getDTOList(String jsonString, Class clazz){   
  79.         setDataFormat2JAVA();   
  80.         JSONArray array = JSONArray.fromObject(jsonString);   
  81.         List list = new ArrayList();   
  82. for(Iterator iter = array.iterator(); iter.hasNext();){   
  83.             JSONObject jsonObject = (JSONObject)iter.next();   
  84.             list.add(JSONObject.toBean(jsonObject, clazz));   
  85.         }   
  86. return list;   
  87.     }   
  88. /**
  89.      * 从一个JSON数组得到一个java对象集合,其中对象中包含有集合属性
  90.      * @param object
  91.      * @param clazz
  92.      * @param map 集合属性的类型
  93.      * @return
  94.      */
  95. public static List getDTOList(String jsonString, Class clazz, Map map){   
  96.         setDataFormat2JAVA();   
  97.         JSONArray array = JSONArray.fromObject(jsonString);   
  98.         List list = new ArrayList();   
  99. for(Iterator iter = array.iterator(); iter.hasNext();){   
  100.             JSONObject jsonObject = (JSONObject)iter.next();   
  101.             list.add(JSONObject.toBean(jsonObject, clazz, map));   
  102.         }   
  103. return list;   
  104.     }   
  105. /**
  106.      * 从json HASH表达式中获取一个map,该map支持嵌套功能
  107.      * 形如:{"id" : "johncon", "name" : "小强"}
  108.      * 注意commons-collections版本,必须包含org.apache.commons.collections.map.MultiKeyMap
  109.      * @param object
  110.      * @return
  111.      */
  112. public static Map getMapFromJson(String jsonString) {   
  113.         setDataFormat2JAVA();   
  114.         JSONObject jsonObject = JSONObject.fromObject(jsonString);   
  115.         Map map = new HashMap();   
  116. for(Iterator iter = jsonObject.keys(); iter.hasNext();){   
  117.             String key = (String)iter.next();   
  118.             map.put(key, jsonObject.get(key));   
  119.         }   
  120. return map;   
  121.     }   
  122. /**
  123.      * 从json数组中得到相应java数组
  124.      * json形如:["123", "456"]
  125.      * @param jsonString
  126.      * @return
  127.      */
  128. public static Object[] getObjectArrayFromJson(String jsonString) {   
  129.         JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  130. return jsonArray.toArray();   
  131.     }   
  132. /**
  133.      * 把数据对象转换成json字符串
  134.      * DTO对象形如:{"id" : idValue, "name" : nameValue, ...}
  135.      * 数组对象形如:[{}, {}, {}, ...]
  136.      * map对象形如:{key1 : {"id" : idValue, "name" : nameValue, ...}, key2 : {}, ...}
  137.      * @param object
  138.      * @return
  139.      */
  140. public static String getJSONString(Object object) throws Exception{   
  141.         String jsonString = null;   
  142. //日期值处理器
  143.         JsonConfig jsonConfig = new JsonConfig();   
  144.         jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor());   
  145. if(object != null){   
  146. if(object instanceof Collection || object instanceof Object[]){   
  147.                 jsonString = JSONArray.fromObject(object, jsonConfig).toString();   
  148.             }else{   
  149.                 jsonString = JSONObject.fromObject(object, jsonConfig).toString();   
  150.             }   
  151.         }   
  152. return jsonString == null ? "{}" : jsonString;   
  153.     }  
原文地址:https://www.cnblogs.com/lichunyang321/p/6689301.html