자바 에서 json 의 직렬 화 와 반 직렬 화

3907 단어 자바json
     /**
     *                       ,                , Teacher  ,  List<Teacher>   
     * @param source   json     
     * @param beanClass       
     * @return    
     */
    public static Object getObjFromJsonArrStr(String source,Class beanClass)
    {
        JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(source);
        return  JSONObject.toBean(jsonObject,beanClass);        
    }
    /**
     *                       ,               , Teacher  ,  List<Teacher>  
     * @param jsonArrStr
     * @param clazz
     * @param classMap
     * @return
     */
    public static Object getObjFromJsonArrStr(String jsonArrStr, Class clazz, Map classMap) 
    {  
        JSONObject jsonObj = JSONObject.fromObject(jsonArrStr);  
                return JSONObject.toBean(jsonObj, clazz, classMap);            
    }
 
     
    /**
     *  string   listBean
     * @param jsonArrStr           
     * @param clazz          
     * @return   list
     */
    @SuppressWarnings("unchecked")
    public static List getListFromJsonArrStr(String jsonArrStr, Class clazz) {  
        JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);  
        List list = new ArrayList();  
        for (int i = 0; i < jsonArr.size(); i++) 
        {  
            list.add(JSONObject.toBean(jsonArr.getJSONObject(i), clazz));  
        }  
        return list;  
    } 
 
    /**
     *  string   listBean            List<Student>  Student     List<Teacher>
     * @param jsonArrStr           
     * @param clazz        
     * @param classMap         Teacher     Map ,   map.put("teacher",Teacher.class)
     * @return          
     *     :
        Map classMap = new HashMap();
        //    Parent           
        Teacher p = new Teacher();
        classMap.put("teacher", p.getClass());
        List mlist = JSONTransfer.getListFromJsonArrStr(resultStr, Student.class, classMap);
     */
    @SuppressWarnings("unchecked")
    public static List getListFromJsonArrStr(String jsonArrStr, Class clazz, Map classMap) 
    {  
       JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);  
       List list = new ArrayList();  
       for (int i = 0; i < jsonArr.size(); i++) 
       {           
           list.add(JSONObject.toBean(jsonArr.getJSONObject(i), clazz, classMap));  
       }  
       return list;  
    }
         
    /**
     *      ,        ,  list,   list       list,         String  
     * @param obj          
     * @return          
     */
    @SuppressWarnings("unchecked")
    public static String getJsonArrStrFromList(Object obj) 
    { 
        //    
        String jsonStr = null;  
        //  
        if (obj == null) {  
            return "{}";  
        }  
        //Json        
        JsonConfig jsonCfg = new JsonConfig();  
   
        //         
        jsonCfg.registerJsonValueProcessor(java.util.Date.class,  
                new JsonDateValueProcessor(SystemConstants.DateFormat));  
         
        //     list
        if (obj instanceof Collection || obj instanceof Object[]) {  
            jsonStr = JSONArray.fromObject(obj, jsonCfg).toString();  
        } else {  
            jsonStr = JSONObject.fromObject(obj, jsonCfg).toString();  
        }     
        return jsonStr; 
    } 
 발췌:http://www.cnblogs.com/luckyliu/archive/2012/04/05/2433682.html

좋은 웹페이지 즐겨찾기