JSONObject(net.sf.json)简介

依赖的jar包:

ezmorph-1.0.6.jar 
commons-lang 2.4 
commons-beanutils 1.7.0 
commons-collections 3.2 
commons-logging 1.1.1


String to JSONObject

JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str );
JSONArray jsonArray = (JSONArray ) JSONSerializer.toJSON( str );

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值