首页>代码>Json转换JavaBean>/1002166899033088.java
package com.yuling.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;




public class JsonToJavaBean {

	/**页面传至后台时,json数据在request的参数名称*/    
    public final static String JSON_ATTRIBUTE = "json";     
    public final static String JSON_ATTRIBUTE1 = "json1";     
    public final static String JSON_ATTRIBUTE2 = "json2";     
    public final static String JSON_ATTRIBUTE3 = "json3";     
    public final static String JSON_ATTRIBUTE4 = "json4";     
         
    /**   
     * 从一个JSON 对象字符格式中得到一个java对象,形如:   
     * {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}}   
     * @param object   
     * @param clazz   
     * @return   
     */    
    @SuppressWarnings("unchecked")
	public static Object getDTO(String jsonString, Class clazz){     
        JSONObject jsonObject = null;     
        try{          
            jsonObject = JSONObject.fromObject(jsonString);     
        }catch(Exception e){     
            e.printStackTrace();     
        }     
        return JSONObject.toBean(jsonObject, clazz);     
    } 
    
    /**
     * json字符串转化我list对象。
     * @param jsonString
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
	public static List getDTOList(String jsonString, Class clazz){         
        JSONArray array = JSONArray.fromObject(jsonString);     
        List list = new ArrayList();     
        for(Iterator iter = array.iterator(); iter.hasNext();){     
            JSONObject jsonObject = (JSONObject)iter.next();     
            list.add(JSONObject.toBean(jsonObject, clazz));     
        }     
        return list;     
    }      
    
}
最近下载更多
zdl638  LV6 2020年5月8日
wyx065747  LV67 2017年11月18日
StevenXu  LV13 2016年1月6日
spring4.0  LV26 2015年4月14日
huangdongpeng  LV3 2014年8月25日
骑着猪猪去逛街  LV32 2013年12月27日
最代码官方  LV167 2012年10月12日
最近浏览更多
Tg171017  LV12 2023年7月20日
1234mama  LV19 2022年4月13日
503382513  LV10 2022年1月21日
小白jmhuang  LV12 2021年5月19日
csy1209457788  LV3 2020年6月10日
jp6863655  LV13 2020年6月10日
zdl638  LV6 2020年5月8日
JinghanHe  LV8 2020年2月24日
jaflkjlkfdjl  LV6 2019年11月11日
陆信乾  LV6 2019年8月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友