相关文章推荐
彷徨的仙人掌  ·  Java ...·  1 周前    · 
发财的包子  ·  ForEach-Object ...·  7 月前    · 
幸福的柳树  ·  call symput与call ...·  1 年前    · 
public class JSONUtil2 { private static final Log log = LogFactory.getLog(JSONUtil2. class ); * 向客户端输出JSON public static void outputJson(HttpServletResponse response, JSONObject json) throws Exception { response.setCharacterEncoding( "UTF-8" ); PrintWriter out = response.getWriter(); out.write(json.toString()); out.flush(); * 从一个JSON 对象字符格式中得到一个java对象 public static Object toBean(String jsonString, Class beanClass) { JSONObject jsonObject = JSONObject.fromObject(jsonString); Object object = JSONObject.toBean(jsonObject, beanClass); return object; public static Object toBean(String jsonString, Class beanClass,Map map) { JSONObject jsonObject = JSONObject.fromObject(jsonString); Object object = JSONObject.toBean(jsonObject, beanClass,map); return object; * 从json对象集合表达式中得到一个java对象列表 * @param jsonString:数组字符串[{key1:value1,key2:value2},{bean2},{bean3},...] * @param pojoClass * @return public static List toList(String jsonString, Class beanClass) { JSONArray jsonArray = JSONArray.fromObject(jsonString); List list = new ArrayList(); for ( int i = 0; i < jsonArray.size(); i++ ) { JSONObject jsonObject = jsonArray.getJSONObject(i); Object object = JSONObject.toBean(jsonObject, beanClass); list.add(object); return list; public static List toList(String jsonString, Class beanClass,Map map) { JSONArray jsonArray = JSONArray.fromObject(jsonString); List list = new ArrayList(); for ( int i = 0; i < jsonArray.size(); i++ ) { JSONObject jsonObject = jsonArray.getJSONObject(i); Object object = JSONObject.toBean(jsonObject, beanClass,map); list.add(object); return list; * 从json字符串中获取一个map,该map支持嵌套功能 * @param jsonString * @return public static Map toMap(String jsonString) { JSONObject jsonObject = JSONObject.fromObject(jsonString); Iterator it = jsonObject.keys(); Map map = new HashMap(); while (it.hasNext()) { String key = (String) it.next(); Object value = jsonObject.get(key); map.put(key, value); return map; * 从json数组中得到相应java数组 * @param jsonString * @return public static Object[] toObjectArray(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); return jsonArray.toArray(); * 从json解析出java字符串数组 * @param jsonString * @return public static String[] toStringArray(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); String[] stringArray = new String[jsonArray.size()]; for ( int i = 0; i < jsonArray.size(); i++ ) { stringArray[i] = jsonArray.getString(i); return stringArray; * 从json解析出javaLong型对象数组 * @param jsonString * @return public static Long[] toLongArray(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); Long[] longArray = new Long[jsonArray.size()]; for ( int i = 0; i < jsonArray.size(); i++ ) { longArray[i] = jsonArray.getLong(i); return longArray; * 从json解析出java Integer型对象数组 * @param jsonString:[1,2,3,4] * @return public static Integer[] toIntegerArray(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); Integer[] integerArray = new Integer[jsonArray.size()]; for ( int i = 0; i < jsonArray.size(); i++ ) { integerArray[i] = jsonArray.getInt(i); return integerArray; * 从json中解析出java Double型对象数组 * @param jsonString * @return public static Double[] toDoubleArray(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); Double[] doubleArray = new Double[jsonArray.size()]; for ( int i = 0; i < jsonArray.size(); i++ ) { doubleArray[i] = jsonArray.getDouble(i); return doubleArray; * 将java对象转换成json字符串 * @param javaObj * @return public static String toJsonString(Object object) { JSONObject json = JSONObject.fromObject(object); return json.toString(); * 将java对象转化为json数组字符串 * [{"name":"name1","id":"id1"},{"name":"name2","id":"id2"}] public static String toJsonArrayString(List<Object> list) { JSONArray jsonArray = JSONArray.fromObject(list); return jsonArray.toString();
package com.sc.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.beanutils.BeanUtils;
import com.sc.bean.MyTeam;
import com.sc.bean.OnethinkUser;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONUtils {
     * @author 16616
     * @date 2018-01-13
     * (1)将list对象序列化为JSON文本
     * @param list:list对象
     * @return
    public static <T> String toJSONString(List<T> list){
        JSONArray jsonArray=JSONArray.fromObject(list);
        return jsonArray.toString();
     * (2)将对象序列化为JSON文本
     * @param object:对象
     * @return
    public static String toJSONString(Object object){
        JSONArray jsonArray=JSONArray.fromObject(object);
        return jsonArray.toString();
     * (3)将JSON对象数组序列化为JSON文本
     * @param jsonArray:json对象数组
     * @return
    public static String toJSONString(JSONArray jsonArray){
        return jsonArray.toString();
     * (4)将JSON对象序列化为JSON文本
     * @param jsonObject
     * @return
    public static String toJSONString(JSONObject jsonObject){
        return jsonObject.toString();
     * (5)将对象转换为List对象(先将对象序列化为JSON对象数组,再将json对象数组转换为List对象)
     * @param object:对象
     * @return
    public static List toArrayList(Object object){
        List arrayList=new ArrayList();
        //将对象序列化为JSON对象数组
        JSONArray jsonArray=JSONArray.fromObject(object);
        //将json对象数组转换为List对象
        Iterator it=jsonArray.iterator();
        while(it.hasNext()){
            JSONObject jsonObject=(JSONObject) it.next();
            Iterator keys=jsonObject.keys();
            while(keys.hasNext()){
                Object key=keys.next();
                Object value=jsonObject.get(key);
                arrayList.add(value);
        return arrayList;
     * (6)将对象转换为Collection对象(先将对象序列化为JSON对象数组,再将json对象数组转换为Collection对象)
     * @param object:对象
     * @return
    public static Collection toCollection(Object object){
        //将对象序列化为JSON对象数组
        JSONArray jsonArray=JSONArray.fromObject(object);
        return JSONArray.toCollection(jsonArray);
     * (7)将对象转换为JSON对象数组
     * @param object:对象
     * @return
    public static JSONArray toJSONArray(Object object){
        return JSONArray.fromObject(object);
     * (8)将对象转换为JSON对象
     * @param object:对象
     * @return
    public static JSONObject toJSONObject(Object object){
        return JSONObject.fromObject(object);
     * (9)将对象转换为HashMap
     * @param object:对象
     * @return
    public static HashMap toHashMap(Object object){
        HashMap<String, Object> data=new HashMap<String, Object>();
        /*JSONObject jsonObject=JSONObject.fromObject(object);*/
        JSONObject jsonObject=JSONUtils.toJSONObject(object);
        Iterator it=jsonObject.keys();
        while(it.hasNext()){
            String key=String.valueOf(it.next());
            Object value=jsonObject.get(key);
            data.put(key, value);
        return data;
     * (10)将对象转换为List(返回非实体类型(Map)的List
     * @param object:对象
     * @return
    public static List<Map<String,Object>> toList(Object object){
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        JSONArray jsonArray=JSONArray.fromObject(object);
        for(Object obj:jsonArray){
            JSONObject jsonObject=(JSONObject) obj;
            Map<String, Object> map=new HashMap<String, Object>();
            Iterator it=jsonObject.keys();
            while(it.hasNext()){
                String key=(String) it.next();
                Object value=jsonObject.get(key);
                map.put(key, value);
            list.add(map);
        return list;
     * (11)将JSON对象数组转换为传入类型的List
     * @param jsonArray:json对象数组
     * @param objectClass:要转换成的list类型
     * @return
    public static <T> List<T> toList(JSONArray jsonArray,Class<T> objectClass){
        return JSONArray.toList(jsonArray, objectClass);
     * (12)将对象转换为传入类型的List(先将对象转换为JSON对象,再按照上面的方法转换)
     * @param object:对象
     * @param objectClass:要转换成的list类型
     * @return
    public static <T> List<T> toList(Object object,Class<T> objectClass){
        JSONArray jsonArray=JSONArray.fromObject(object);
        return JSONArray.toList(jsonArray, objectClass);
     * (13)将JSON对象转换为传入类型的对象
     * @param jsonObject:json对象
     * @param beanClass:要转换成的对象的类型
     * @return
    public static <T> T toBean(JSONObject jsonObject,Class<T> beanClass){
        return (T) JSONObject.toBean(jsonObject, beanClass);
     * (14)将对象转换为传入类型的对象(先将对象转换为JSON对象,再按照上面的方法)
     * @param object:对象
     * @param beanClass:要转换成的对象的类型
     * @return
    public static <T> T toBean(Object object,Class<T> beanClass){
        JSONObject jsonObject=JSONObject.fromObject(object);
        return (T) JSONObject.toBean(jsonObject, beanClass);
     * (15)将JSON文本反序列化为主从关系的实体 
     * @param 泛型T 代表主实体类型
     * @param 泛型D 代表从实体类型
     * @param jsonString:JSON文本
     * @param mainClass:主实体类型
     * @param detailName:从实体类在主实体类中的属性名称
     * @param detailClass:从实体类型
     * @return
    public static <T,D> T toBean(String jsonString,Class<T> mainClass,
            String detailName,Class<D> detailClass){
        //将json文本转换成json对象
        JSONObject jsonObject=JSONObject.fromObject(jsonString);
        //获得json对象中从实体类的json对象
        JSONArray jsonArray=(JSONArray) jsonObject.get(detailName);
        //将JSON对象转换为主实体类型
        T mainEntity=JSONUtils.toBean(jsonObject, mainClass);
        //将JSON对象数组转换为从实体类型的list
        List<D> detailList=JSONUtils.toList(jsonArray, detailClass);
        //利用BeanUtils,放入对象中
        try {
            BeanUtils.setProperty(mainEntity, detailName, detailList);
        } catch (Exception  e) {
            throw new RuntimeException("主从关系JSON反序列化实体失败!");
        return mainEntity;
    /**(16)将JSON文本反序列化为主从关系的实体
     * @param 泛型T 代表主实体类型
     * @param 泛型D1 代表从实体类型
     * @param 泛型D2 代表从实体类型
     * @param jsonString:json文本
     * @param mainClass:主实体类性
     * @param detailName1:从实体类在主实体类中的属性
     * @param detailClass1:从实体类型
     * @param detailName2:从实体类在主实体类中的属性
     * @param detailClass2:从实体类型
     * @return
    public static <T,D1,D2> T toBean(String jsonString,Class<T> mainClass,
            String detailName1,Class<D1> detailClass1,String detailName2,
            Class<D2> detailClass2){
        JSONObject jsonObject=JSONObject.fromObject(jsonString);
        JSONArray jsonArray1=(JSONArray) jsonObject.get(detailName1);
        JSONArray jsonArray2=(JSONArray) jsonObject.get(detailName2);
        T mainEntity=JSONUtils.toBean(jsonObject, mainClass);
        List<D1> detailList1=JSONUtils.toList(jsonArray1, detailClass1);
        List<D2> detailList2=JSONUtils.toList(jsonArray1, detailClass2);
        try {
            BeanUtils.setProperty(mainEntity, detailName1, detailList1);
            BeanUtils.setProperty(mainEntity, detailName2, detailList2);
        } catch (Exception ex){
            throw new RuntimeException("主从关系JSON反序列化实体失败!");
        return mainEntity;
     * (17)将JSON文本反序列化为主从关系的实体
     * @param 泛型T 代表主实体类型
     * @param 泛型D1 代表从实体类型
     * @param 泛型D2 代表从实体类型
     * @param jsonString:json文本
     * @param mainClass:主实体类型
     * @param detailName1:从实体类在主实体类中的属性
     * @param detailClass1:从实体类型
     * @param detailName2:从实体类在主实体类中的属性
     * @param detailClass2:从实体类型
     * @param detailName3:从实体类在主实体类中的属性
     * @param detailClass3:从实体类型
     * @return
    public static <T,D1,D2,D3> T toBean(String jsonString,Class<T> mainClass, String detailName1, Class<D1> detailClass1,
            String detailName2, Class<D2> detailClass2, String detailName3,
            Class<D3> detailClass3){
        JSONObject jsonObject = JSONObject.fromObject(jsonString);
        JSONArray jsonArray1 = (JSONArray) jsonObject.get(detailName1);
        JSONArray jsonArray2 = (JSONArray) jsonObject.get(detailName2);
        JSONArray jsonArray3 = (JSONArray) jsonObject.get(detailName3);
        T mainEntity = JSONUtils.toBean(jsonObject, mainClass);
        List<D1> detailList1 = JSONUtils.toList(jsonArray1, detailClass1);
        List<D2> detailList2 = JSONUtils.toList(jsonArray2, detailClass2);
        List<D3> detailList3 = JSONUtils.toList(jsonArray3, detailClass3);
            BeanUtils.setProperty(mainEntity, detailName1, detailList1);
            BeanUtils.setProperty(mainEntity, detailName2, detailList2);
            BeanUtils.setProperty(mainEntity, detailName3, detailList3);
        catch (Exception ex)
            throw new RuntimeException("主从关系JSON反序列化实体失败!");
        return mainEntity;
     * (18)将JSON文本反序列化为主从关系的实体
     * @param jsonString:json文本
     * @param mainClass:主实体类型
     * @param detailClass:存放了多个从实体在主实体中属性名称和类型
     * @return
    public static <T> T toBean(String jsonString, Class<T> mainClass,
            HashMap<String, Class> detailClass)
        JSONObject jsonObject = JSONObject.fromObject(jsonString);
        T mainEntity = JSONUtils.toBean(jsonObject, mainClass);
        for (Object key : detailClass.keySet())
                Class value = (Class) detailClass.get(key);
                BeanUtils.setProperty(mainEntity, key.toString(), value);
            catch (Exception ex)
                throw new RuntimeException("主从关系JSON反序列化实体失败!");
        return mainEntity;
    public static List<MyTeam> jsonToObjectList(String jsonString){
        List<MyTeam> myTeamList=new ArrayList<MyTeam>();
         JSONObject jsonObject = JSONObject.fromObject(jsonString);
         //取出json中的data数据
         JSONObject data = jsonObject.getJSONObject("data");
         JSONObject dataJsonObject=JSONObject.fromObject(data);  
         Map<String, Object> map =dataJsonObject;  
         for (Entry<String, Object> entry : map.entrySet()) {  
             MyTeam myTeam=new MyTeam();
             myTeam.setDaiShu(Integer.parseInt(entry.getKey()));
             //将其转换为对象
             OnethinkUser onethinkUser=(OnethinkUser)JSONObject.toBean((JSONObject) entry.getValue(), OnethinkUser.class);
             myTeam.setOnethinkUser(onethinkUser);
             myTeamList.add(myTeam);
        return myTeamList;
package com.sc.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONUtils1 {
     * 将string转换成listBean 
     * @param jsonArrStr 需要反序列化的字符串 
     * @param clazz 被反序列化之后的类 
     * @return 实体list 
    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;    

资源链接:链接:https://pan.baidu.com/s/1nw6UEuP 密码:uj4v