在JAVA中用json-lib-2.3-jdk15.jar包中提供了JSONObject和JSONArray基类,用于JSON的序列化和反序列化的操作。但是我们更习惯将其进一步封装,达到更好的重用。

封装后的JSON工具类JSONUtils.java代码如下:

  1. import java.util.ArrayList;
  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.Map;
  7. import net.sf.json.JSONArray;
  8. import net.sf.json.JSONObject;
  9. import org.apache.commons.beanutils.BeanUtils;

  10. public class JSONUtils
  11. {
  12.     /**
  13.      *
  14.      * @author wangwei JSON工具类
  15.      * @param
  16.      *
  17.      */
  18.     
  19.     /***
  20.      * 将List对象序列化为JSON文本
  21.      */
  22.     public static <T> String toJSONString(List<T> list)
  23.     {
  24.         JSONArray jsonArray = JSONArray.fromObject(list);

  25.         return jsonArray.toString();
  26.     }
  27.     
  28.     /***
  29.      * 将对象序列化为JSON文本
  30.      * @param object
  31.      * @return
  32.      */
  33.     public static String toJSONString(Object object)
  34.     {
  35.         JSONArray jsonArray = JSONArray.fromObject(object);

  36.         return jsonArray.toString();
  37.     }

  38.     /***
  39.      * 将JSON对象数组序列化为JSON文本
  40.      * @param jsonArray
  41.      * @return
  42.      */
  43.     public static String toJSONString(JSONArray jsonArray)
  44.     {
  45.         return jsonArray.toString();
  46.     }

  47.     /***
  48.      * 将JSON对象序列化为JSON文本
  49.      * @param jsonObject
  50.      * @return
  51.      */
  52.     public static String toJSONString(JSONObject jsonObject)
  53.     {
  54.         return jsonObject.toString();
  55.     }
  56.     
  57.     /***
  58.      * 将对象转换为List对象
  59.      * @param object
  60.      * @return
  61.      */
  62.     public static List toArrayList(Object object)
  63.     {
  64.         List arrayList = new ArrayList();

  65.         JSONArray jsonArray = JSONArray.fromObject(object);

  66.         Iterator it = jsonArray.iterator();
  67.         while (it.hasNext())
  68.         {
  69.             JSONObject jsonObject = (JSONObject) it.next();

  70.             Iterator keys = jsonObject.keys();
  71.             while (keys.hasNext())
  72.             {
  73.                 Object key = keys.next();
  74.                 Object value = jsonObject.get(key);
  75.                 arrayList.add(value);
  76.             }
  77.         }

  78.         return arrayList;
  79.     }

  80.     /***
  81.      * 将对象转换为Collection对象
  82.      * @param object
  83.      * @return
  84.      */
  85.     public static Collection toCollection(Object object)
  86.     {
  87.         JSONArray jsonArray = JSONArray.fromObject(object);

  88.         return JSONArray.toCollection(jsonArray);
  89.     }

  90.     /***
  91.      * 将对象转换为JSON对象数组
  92.      * @param object
  93.      * @return
  94.      */
  95.     public static JSONArray toJSONArray(Object object)
  96.     {
  97.         return JSONArray.fromObject(object);
  98.     }

  99.     /***
  100.      * 将对象转换为JSON对象
  101.      * @param object
  102.      * @return
  103.      */
  104.     public static JSONObject toJSONObject(Object object)
  105.     {
  106.         return JSONObject.fromObject(object);
  107.     }

  108.     /***
  109.      * 将对象转换为HashMap
  110.      * @param object
  111.      * @return
  112.      */
  113.     public static HashMap toHashMap(Object object)
  114.     {
  115.         HashMap<String, Object> data = new HashMap<String, Object>();
  116.         JSONObject jsonObject = JSONUtils.toJSONObject(object);
  117.         Iterator it = jsonObject.keys();
  118.         while (it.hasNext())
  119.         {
  120.             String key = String.valueOf(it.next());
  121.             Object value = jsonObject.get(key);
  122.             data.put(key, value);
  123.         }

  124.         return data;
  125.     }

  126.     /***
  127.      * 将对象转换为List>
  128.      * @param object
  129.      * @return
  130.      */
  131.     // 返回非实体类型(Map)的List
  132.     public static List<Map<String, Object>> toList(Object object)
  133.     {
  134.         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  135.         JSONArray jsonArray = JSONArray.fromObject(object);
  136.         for (Object obj : jsonArray)
  137.         {
  138.             JSONObject jsonObject = (JSONObject) obj;
  139.             Map<String, Object> map = new HashMap<String, Object>();
  140.             Iterator it = jsonObject.keys();
  141.             while (it.hasNext())
  142.             {
  143.                 String key = (String) it.next();
  144.                 Object value = jsonObject.get(key);
  145.                 map.put((String) key, value);
  146.             }
  147.             list.add(map);
  148.         }
  149.         return list;
  150.     }

  151.     /***
  152.      * 将JSON对象数组转换为传入类型的List
  153.      * @param
  154.      * @param jsonArray
  155.      * @param objectClass
  156.      * @return
  157.      */
  158.     public static <T> List<T> toList(JSONArray jsonArray, Class<T> objectClass)
  159.     {
  160.         return JSONArray.toList(jsonArray, objectClass);
  161.     }

  162.     /***
  163.      * 将对象转换为传入类型的List
  164.      * @param
  165.      * @param jsonArray
  166.      * @param objectClass
  167.      * @return
  168.      */
  169.     public static <T> List<T> toList(Object object, Class<T> objectClass)
  170.     {
  171.         JSONArray jsonArray = JSONArray.fromObject(object);

  172.         return JSONArray.toList(jsonArray, objectClass);
  173.     }

  174.     /***
  175.      * 将JSON对象转换为传入类型的对象
  176.      * @param
  177.      * @param jsonObject
  178.      * @param beanClass
  179.      * @return
  180.      */
  181.     public static <T> T toBean(JSONObject jsonObject, Class<T> beanClass)
  182.     {
  183.         return (T) JSONObject.toBean(jsonObject, beanClass);
  184.     }

  185.     /***
  186.      * 将将对象转换为传入类型的对象
  187.      * @param
  188.      * @param object
  189.      * @param beanClass
  190.      * @return
  191.      */
  192.     public static <T> T toBean(Object object, Class<T> beanClass)
  193.     {
  194.         JSONObject jsonObject = JSONObject.fromObject(object);

  195.         return (T) JSONObject.toBean(jsonObject, beanClass);
  196.     }

  197.     /***
  198.      * 将JSON文本反序列化为主从关系的实体
  199.      * @param 泛型T 代表主实体类型
  200.      * @param 泛型D 代表从实体类型
  201.      * @param jsonString JSON文本
  202.      * @param mainClass 主实体类型
  203.      * @param detailName 从实体类在主实体类中的属性名称
  204.      * @param detailClass 从实体类型
  205.      * @return
  206.      */
  207.     public static <T, D> T toBean(String jsonString, Class<T> mainClass,
  208.             String detailName, Class<D> detailClass)
  209.     {
  210.         JSONObject jsonObject = JSONObject.fromObject(jsonString);
  211.         JSONArray jsonArray = (JSONArray) jsonObject.get(detailName);

  212.         T mainEntity = JSONUtils.toBean(jsonObject, mainClass);
  213.         List<D> detailList = JSONUtils.toList(jsonArray, detailClass);

  214.         try
  215.         {
  216.             BeanUtils.setProperty(mainEntity, detailName, detailList);
  217.         }
  218.         catch (Exception ex)
  219.         {
  220.             throw new RuntimeException("主从关系JSON反序列化实体失败!");
  221.         }

  222.         return mainEntity;
  223.     }

  224.     /***
  225.      * 将JSON文本反序列化为主从关系的实体
  226.      * @param 泛型T 代表主实体类型
  227.      * @param 泛型D1 代表从实体类型
  228.      * @param 泛型D2 代表从实体类型
  229.      * @param jsonString JSON文本
  230.      * @param mainClass 主实体类型
  231.      * @param detailName1 从实体类在主实体类中的属性
  232.      * @param detailClass1 从实体类型
  233.      * @param detailName2 从实体类在主实体类中的属性
  234.      * @param detailClass2 从实体类型
  235.      * @return
  236.      */
  237.     public static <T, D1, D2> T toBean(String jsonString, Class<T> mainClass,
  238.             String detailName1, Class<D1> detailClass1, String detailName2,
  239.             Class<D2> detailClass2)
  240.     {
  241.         JSONObject jsonObject = JSONObject.fromObject(jsonString);
  242.         JSONArray jsonArray1 = (JSONArray) jsonObject.get(detailName1);
  243.         JSONArray jsonArray2 = (JSONArray) jsonObject.get(detailName2);

  244.         T mainEntity = JSONUtils.toBean(jsonObject, mainClass);
  245.         List<D1> detailList1 = JSONUtils.toList(jsonArray1, detailClass1);
  246.         List<D2> detailList2 = JSONUtils.toList(jsonArray2, detailClass2);

  247.         try
  248.         {
  249.             BeanUtils.setProperty(mainEntity, detailName1, detailList1);
  250.             BeanUtils.setProperty(mainEntity, detailName2, detailList2);
  251.         }
  252.         catch (Exception ex)
  253.         {
  254.             throw new RuntimeException("主从关系JSON反序列化实体失败!");
  255.         }

  256.         return mainEntity;
  257.     }
  258.     
  259.     /***
  260.      * 将JSON文本反序列化为主从关系的实体
  261.      * @param 泛型T 代表主实体类型
  262.      * @param 泛型D1 代表从实体类型
  263.      * @param 泛型D2 代表从实体类型
  264.      * @param jsonString JSON文本
  265.      * @param mainClass 主实体类型
  266.      * @param detailName1 从实体类在主实体类中的属性
  267.      * @param detailClass1 从实体类型
  268.      * @param detailName2 从实体类在主实体类中的属性
  269.      * @param detailClass2 从实体类型
  270.      * @param detailName3 从实体类在主实体类中的属性
  271.      * @param detailClass3 从实体类型
  272.      * @return
  273.      */
  274.     public static <T, D1, D2, D3> T toBean(String jsonString,
  275.             Class<T> mainClass, String detailName1, Class<D1> detailClass1,
  276.             String detailName2, Class<D2> detailClass2, String detailName3,
  277.             Class<D3> detailClass3)
  278.     {
  279.         JSONObject jsonObject = JSONObject.fromObject(jsonString);
  280.         JSONArray jsonArray1 = (JSONArray) jsonObject.get(detailName1);
  281.         JSONArray jsonArray2 = (JSONArray) jsonObject.get(detailName2);
  282.         JSONArray jsonArray3 = (JSONArray) jsonObject.get(detailName3);

  283.         T mainEntity = JSONUtils.toBean(jsonObject, mainClass);
  284.         List<D1> detailList1 = JSONUtils.toList(jsonArray1, detailClass1);
  285.         List<D2> detailList2 = JSONUtils.toList(jsonArray2, detailClass2);
  286.         List<D3> detailList3 = JSONUtils.toList(jsonArray3, detailClass3);

  287.         try
  288.         {
  289.             BeanUtils.setProperty(mainEntity, detailName1, detailList1);
  290.             BeanUtils.setProperty(mainEntity, detailName2, detailList2);
  291.             BeanUtils.setProperty(mainEntity, detailName3, detailList3);
  292.         }
  293.         catch (Exception ex)
  294.         {
  295.             throw new RuntimeException("主从关系JSON反序列化实体失败!");
  296.         }

  297.         return mainEntity;
  298.     }

  299.     /***
  300.      * 将JSON文本反序列化为主从关系的实体
  301.      * @param 主实体类型
  302.      * @param jsonString JSON文本
  303.      * @param mainClass 主实体类型
  304.      * @param detailClass 存放了多个从实体在主实体中属性名称和类型
  305.      * @return
  306.      */
  307.     public static <T> T toBean(String jsonString, Class<T> mainClass,
  308.             HashMap<String, Class> detailClass)
  309.     {
  310.         JSONObject jsonObject = JSONObject.fromObject(jsonString);
  311.         T mainEntity = JSONUtils.toBean(jsonObject, mainClass);
  312.         for (Object key : detailClass.keySet())
  313.         {
  314.             try
  315.             {
  316.                 Class value = (Class) detailClass.get(key);
  317.                 BeanUtils.setProperty(mainEntity, key.toString(), value);
  318.             }
  319.             catch (Exception ex)
  320.             {
  321.                 throw new RuntimeException("主从关系JSON反序列化实体失败!");
  322.             }
  323.         }
  324.         return mainEntity;
  325.     }
  326. }

上面代码中所需的jar包有:json-lib-2.3-jdk15.jar,commons-beanutils-1.8.0.jar

12-21 02:28