• 将json字符串,转换为对象实体


    先说明一下,这个实体中嵌套有多层内部类,json是已经拼好的。

    json字符串为:

    {"QueryMXASSETDTSResponse":{"rsStart":0,"rsCount":2,"rsTotal":2,"MXASSETDTSSet":{"ASSET":[{"ASSETNUM":"D-1001B","DESCRIPTION":"\u5367\u5f0f\u6c14\u6db2\u5206\u79bb\u5668","SITEID":"AMHSITE1","STATUS":"\u64cd\u4f5c","ASSETSPEC":[{"ALNVALUE":"\u91cd\u529b(\u6c14\u6db2)\u5206\u79bb\u5668","ASSETATTRID":"EQUIPN","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u8bbe\u5907\u540d\u79f0","ORGID":"AMHNG"}]},{"ASSETATTRID":"MTN","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u578b\u53f7","ORGID":"AMHNG"}]},{"ALNVALUE":"R208084","ASSETATTRID":"SN","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u7cfb\u5217\u53f7","ORGID":"AMHNG"}]},{"ALNVALUE":"\u539f\u6599\u6c14","ASSETATTRID":"MEDIUM","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u4ecb\u8d28\u7c7b\u578b","ORGID":"AMHNG"}]},{"ALNVALUE":"\u5367\u5f0f\u5706\u7b52","ASSETATTRID":"TYPE","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u578b\u5f0f","ORGID":"AMHNG"}]},{"ALNVALUE":"\u542b\u6c14\u6db2\u5206\u79bb\u5143\u4ef6\uff0c\u79ef\u6db2\u5305\uff1b\u6c14\u6db2\u5206\u79bb","ASSETATTRID":"CAP","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u7ed3\u6784\u4e0e\u7528\u9014","ORGID":"AMHNG"}]},{"ALNVALUE":"8.4","ASSETATTRID":"DPR","LINEARASSETSPECID":0,"MEASUREUNITID":"MPA","ASSETATTRIBUTE":[{"DESCRIPTION":"\u8bbe\u8ba1\u538b\u529b","ORGID":"AMHNG"}]},{"ALNVALUE":"11.33","ASSETATTRID":"TP","LINEARASSETSPECID":0,"MEASUREUNITID":"MPA","ASSETATTRIBUTE":[{"DESCRIPTION":"\u6d4b\u8bd5\u538b\u529b","ORGID":"AMHNG"}]},{"ALNVALUE":"80","ASSETATTRID":"DTE","LINEARASSETSPECID":0,"MEASUREUNITID":"\u2103","ASSETATTRIBUTE":[{"DESCRIPTION":"\u8bbe\u8ba1\u6e29\u5ea6","ORGID":"AMHNG"}]},{"ALNVALUE":"15-35","ASSETATTRID":"WT","LINEARASSETSPECID":0,"MEASUREUNITID":"\u2103","ASSETATTRIBUTE":[{"DESCRIPTION":"\u5de5\u4f5c\u6e29\u5ea6","ORGID":"AMHNG"}]},{"ALNVALUE":"8","ASSETATTRID":"WP","LINEARASSETSPECID":0,"MEASUREUNITID":"MPA","ASSETATTRIBUTE":[{"DESCRIPTION":"\u5de5\u4f5c\u538b\u529b","ORGID":"AMHNG"}]},{"ASSETATTRID":"IND","LINEARASSETSPECID":0,"MEASUREUNITID":"MM","ASSETATTRIBUTE":[{"DESCRIPTION":"\u5185\u5f84","ORGID":"AMHNG"}]},{"ASSETATTRID":"ED","LINEARASSETSPECID":0,"MEASUREUNITID":"MM","ASSETATTRIBUTE":[{"DESCRIPTION":"\u5916\u5f84","ORGID":"AMHNG"}]},{"ASSETATTRID":"WAT","LINEARASSETSPECID":0,"MEASUREUNITID":"MM","ASSETATTRIBUTE":[{"DESCRIPTION":"\u58c1\u539a","ORGID":"AMHNG"}]},{"ALNVALUE":"20R\n\u6b63\u706b","ASSETATTRID":"BM","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u4e3b\u4f53\u6750\u6599","ORGID":"AMHNG"}]},{"ALNVALUE":"4","ASSETATTRID":"RL","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u8150\u8680\u88d5\u5ea6","ORGID":"AMHNG"}]},{"ASSETATTRID":"HPT","LINEARASSETSPECID":0,"MEASUREUNITID":"MM","ASSETATTRIBUTE":[{"DESCRIPTION":"\u4fdd\u6e29\u539a\u5ea6","ORGID":"AMHNG"}]},{"ALNVALUE":"\u805a\u915a\u919b\u677f\u6750","ASSETATTRID":"HPM","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u4fdd\u6e29\u6750\u6599","ORGID":"AMHNG"}]},{"ASSETATTRID":"AM","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u9632\u8150\u63aa\u65bd","ORGID":"AMHNG"}]},{"ALNVALUE":"17.54","ASSETATTRID":"CU","LINEARASSETSPECID":0,"MEASUREUNITID":"M3","ASSETATTRIBUTE":[{"DESCRIPTION":"\u5bb9\u79ef","ORGID":"AMHNG"}]},{"ALNVALUE":"\u4e0a:\u03a61600\u00d772\u00d77044\u4e0b:800\u00d736\u00d77295","ASSETATTRID":"DIM","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u89c4\u683c\u5c3a\u5bf8","ORGID":"AMHNG"}]},{"ALNVALUE":"37200","ASSETATTRID":"WEIGHT","LINEARASSETSPECID":0,"MEASUREUNITID":"KG","ASSETATTRIBUTE":[{"DESCRIPTION":"\u91cd\u91cf","ORGID":"AMHNG"}]},{"ALNVALUE":"39783","ASSETATTRID":"MD","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u751f\u4ea7\u65e5\u671f","ORGID":"AMHNG"}]},{"ALNVALUE":"2","ASSETATTRID":"PVE","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u5bb9\u5668\u7c7b\u522b","ORGID":"AMHNG"}]},{"ALNVALUE":"\u4e2d\u56fd\u77f3\u6cb9\u96c6\u56e2\u5de5\u7a0b\u8bbe\u8ba1\u6709\u9650\u8d23\u4efb\u516c\u53f8\u897f\u5357\u5206\u516c\u53f8","ASSETATTRID":"DESIGNER","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u8bbe\u8ba1\u5355\u4f4d","ORGID":"AMHNG"}]},{"ALNVALUE":"\u56db\u5ddd\u77f3\u6cb9\u5929\u7136\u6c14\u5efa\u8bbe\u5de5\u7a0b\u6709\u9650\u8d23\u4efb\u516c\u53f8\uff08\u5bb9\u5668\u5236\u9020\u5382\uff09","ASSETATTRID":"MFRN","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u751f\u4ea7\u5382\u5546","ORGID":"AMHNG"}]},{"ASSETATTRID":"VENDORN","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u4f9b\u8d27\u5546","ORGID":"AMHNG"}]},{"ASSETATTRID":"REMARK","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u5907\u6ce8","ORGID":"AMHNG"}]}]},{"ASSETNUM":"D-1001C","DESCRIPTION":"\u5367\u5f0f\u6c14\u6db2\u5206\u79bb\u5668","SITEID":"AMHSITE1","STATUS":"\u64cd\u4f5c","ASSETSPEC":[{"ALNVALUE":"\u91cd\u529b(\u6c14\u6db2)\u5206\u79bb\u5668","ASSETATTRID":"EQUIPN","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u8bbe\u5907\u540d\u79f0","ORGID":"AMHNG"}]},{"ASSETATTRID":"MTN","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u578b\u53f7","ORGID":"AMHNG"}]},{"ALNVALUE":"R208081","ASSETATTRID":"SN","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u7cfb\u5217\u53f7","ORGID":"AMHNG"}]},{"ALNVALUE":"\u539f\u6599\u6c14","ASSETATTRID":"MEDIUM","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u4ecb\u8d28\u7c7b\u578b","ORGID":"AMHNG"}]},{"ALNVALUE":"\u5367\u5f0f\u5706\u7b52","ASSETATTRID":"TYPE","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u578b\u5f0f","ORGID":"AMHNG"}]},{"ALNVALUE":"\u542b\u6c14\u6db2\u5206\u79bb\u5143\u4ef6\uff0c\u79ef\u6db2\u5305\uff1b\u6c14\u6db2\u5206\u79bb","ASSETATTRID":"CAP","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u7ed3\u6784\u4e0e\u7528\u9014","ORGID":"AMHNG"}]},{"ALNVALUE":"8.4","ASSETATTRID":"DPR","LINEARASSETSPECID":0,"MEASUREUNITID":"MPA","ASSETATTRIBUTE":[{"DESCRIPTION":"\u8bbe\u8ba1\u538b\u529b","ORGID":"AMHNG"}]},{"ALNVALUE":"11.33","ASSETATTRID":"TP","LINEARASSETSPECID":0,"MEASUREUNITID":"MPA","ASSETATTRIBUTE":[{"DESCRIPTION":"\u6d4b\u8bd5\u538b\u529b","ORGID":"AMHNG"}]},{"ALNVALUE":"80","ASSETATTRID":"DTE","LINEARASSETSPECID":0,"MEASUREUNITID":"\u2103","ASSETATTRIBUTE":[{"DESCRIPTION":"\u8bbe\u8ba1\u6e29\u5ea6","ORGID":"AMHNG"}]},{"ALNVALUE":"15-35","ASSETATTRID":"WT","LINEARASSETSPECID":0,"MEASUREUNITID":"\u2103","ASSETATTRIBUTE":[{"DESCRIPTION":"\u5de5\u4f5c\u6e29\u5ea6","ORGID":"AMHNG"}]},{"ALNVALUE":"8","ASSETATTRID":"WP","LINEARASSETSPECID":0,"MEASUREUNITID":"MPA","ASSETATTRIBUTE":[{"DESCRIPTION":"\u5de5\u4f5c\u538b\u529b","ORGID":"AMHNG"}]},{"ASSETATTRID":"IND","LINEARASSETSPECID":0,"MEASUREUNITID":"MM","ASSETATTRIBUTE":[{"DESCRIPTION":"\u5185\u5f84","ORGID":"AMHNG"}]},{"ASSETATTRID":"ED","LINEARASSETSPECID":0,"MEASUREUNITID":"MM","ASSETATTRIBUTE":[{"DESCRIPTION":"\u5916\u5f84","ORGID":"AMHNG"}]},{"ASSETATTRID":"WAT","LINEARASSETSPECID":0,"MEASUREUNITID":"MM","ASSETATTRIBUTE":[{"DESCRIPTION":"\u58c1\u539a","ORGID":"AMHNG"}]},{"ALNVALUE":"20R\n\u6b63\u706b","ASSETATTRID":"BM","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u4e3b\u4f53\u6750\u6599","ORGID":"AMHNG"}]},{"ALNVALUE":"4","ASSETATTRID":"RL","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u8150\u8680\u88d5\u5ea6","ORGID":"AMHNG"}]},{"ASSETATTRID":"HPT","LINEARASSETSPECID":0,"MEASUREUNITID":"MM","ASSETATTRIBUTE":[{"DESCRIPTION":"\u4fdd\u6e29\u539a\u5ea6","ORGID":"AMHNG"}]},{"ALNVALUE":"\u805a\u915a\u919b\u677f\u6750","ASSETATTRID":"HPM","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u4fdd\u6e29\u6750\u6599","ORGID":"AMHNG"}]},{"ASSETATTRID":"AM","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u9632\u8150\u63aa\u65bd","ORGID":"AMHNG"}]},{"ALNVALUE":"17.54","ASSETATTRID":"CU","LINEARASSETSPECID":0,"MEASUREUNITID":"M3","ASSETATTRIBUTE":[{"DESCRIPTION":"\u5bb9\u79ef","ORGID":"AMHNG"}]},{"ALNVALUE":"\u4e0a:\u03a61600\u00d772\u00d77044\u4e0b:800\u00d736\u00d77295","ASSETATTRID":"DIM","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u89c4\u683c\u5c3a\u5bf8","ORGID":"AMHNG"}]},{"ALNVALUE":"37200","ASSETATTRID":"WEIGHT","LINEARASSETSPECID":0,"MEASUREUNITID":"KG","ASSETATTRIBUTE":[{"DESCRIPTION":"\u91cd\u91cf","ORGID":"AMHNG"}]},{"ALNVALUE":"39783","ASSETATTRID":"MD","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u751f\u4ea7\u65e5\u671f","ORGID":"AMHNG"}]},{"ALNVALUE":"2","ASSETATTRID":"PVE","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u5bb9\u5668\u7c7b\u522b","ORGID":"AMHNG"}]},{"ALNVALUE":"\u4e2d\u56fd\u77f3\u6cb9\u96c6\u56e2\u5de5\u7a0b\u8bbe\u8ba1\u6709\u9650\u8d23\u4efb\u516c\u53f8\u897f\u5357\u5206\u516c\u53f8","ASSETATTRID":"DESIGNER","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u8bbe\u8ba1\u5355\u4f4d","ORGID":"AMHNG"}]},{"ALNVALUE":"\u56db\u5ddd\u77f3\u6cb9\u5929\u7136\u6c14\u5efa\u8bbe\u5de5\u7a0b\u6709\u9650\u8d23\u4efb\u516c\u53f8\uff08\u5bb9\u5668\u5236\u9020\u5382\uff09","ASSETATTRID":"MFRN","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u751f\u4ea7\u5382\u5546","ORGID":"AMHNG"}]},{"ASSETATTRID":"VENDORN","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u4f9b\u8d27\u5546","ORGID":"AMHNG"}]},{"ASSETATTRID":"REMARK","LINEARASSETSPECID":0,"ASSETATTRIBUTE":[{"DESCRIPTION":"\u5907\u6ce8","ORGID":"AMHNG"}]}]}]}}}

    将要转换为的实体:

    package cn.jasgroup.amudraya.domain;
    
    import java.util.List;
    
    import net.sf.json.JSONArray;
    
    import  cn.jasgroup.amudraya.base.commons.utils.JsonUtil;
    
    public  class EquipRemoteData {
    	
    private QueryMxassetDtsResponse querymxassetdtsresponse;
    
    public QueryMxassetDtsResponse getQuerymxassetdtsresponse() {
    	return querymxassetdtsresponse;
    }
    
    public void setQuerymxassetdtsresponse(
    		QueryMxassetDtsResponse querymxassetdtsresponse) {
    	this.querymxassetdtsresponse = querymxassetdtsresponse;
    }
    
    /**  **/
    public static class QueryMxassetDtsResponse{
    	private  String rsstart="";
    	private  String rscount="";
    	private String rstotal="";
    	private Mxassetdtsset mxassetdtsset;
    	
    	
    	public String getRsstart() {
    		return rsstart;
    	}
    
    
    	public void setRsstart(String rsstart) {
    		this.rsstart = rsstart.toUpperCase();
    	}
    
    
    	public String getRscount() {
    		return rscount;
    	}
    
    
    	public void setRscount(String rscount) {
    		this.rscount = rscount.toUpperCase();
    	}
    
    
    	public String getRstotal() {
    		return rstotal;
    	}
    
    
    	public void setRstotal(String rstotal) {
    		this.rstotal = rstotal.toUpperCase();
    	}
    
    
    	public Mxassetdtsset getMxassetdtsset() {
    		return mxassetdtsset;
    	}
    
    
    	public void setMxassetdtsset(Mxassetdtsset mxassetdtsset) {
    		this.mxassetdtsset = mxassetdtsset;
    	}
    
    
    	/**  **/
    	public static class Mxassetdtsset{
    	   private List<Asset> asset;
    	   
    	   
    	   public Mxassetdtsset() {
    		super();
    	  }
           
    	public List<Asset> getAsset() {
    		return asset;
    	}
    
    	@SuppressWarnings("unchecked")
    	public void setAsset(List<Asset> asset) {
    		System.out.println(JSONArray.fromObject(asset).toString());
    		this.asset=JsonUtil.getDTOList(JSONArray.fromObject(asset).toString(),Asset.class);
    	}
    
    	/**  **/
    	public  static class Asset{
    		  private String assetnum="";
              private String location="";
    		  private String description="";
    		  private String siteid="";
    		  private String status="";
    		  private List<Assetspec> assetspec;
    		 
    		
    		public String getAssetnum() {
    			return assetnum;
    		}
    
    
    		public void setAssetnum(String assetnum) {
    			this.assetnum = assetnum.toUpperCase();
    		}
    
    
    		public String getDescription() {
    			return description.toUpperCase();
    		}
    
    
    		public void setDescription(String description) {
    			this.description = description.toUpperCase();
    		}
    
    
    		public String getSiteid() {
    			return siteid;
    		}
    
    
    		public void setSiteid(String siteid) {
    			this.siteid = siteid.toUpperCase();
    		}
    
    
    		public String getStatus() {
    			return status;
    		}
    
    
    		public void setStatus(String status) {
    			this.status = status.toUpperCase();
    		}
    
    
    		public List<Assetspec> getAssetspec() {
    			return assetspec;
    		}
    
    
    		@SuppressWarnings("unchecked")
    		public void setAssetspec(List<Assetspec> assetspec) {
    			this.assetspec=JsonUtil.getDTOList(JSONArray.fromObject(assetspec).toString(),Assetspec.class);
    		}
            
    
    		public String getLocation() {
    			return location;
    		}
    
    
    		public void setLocation(String location) {
    			this.location = location;
    		}
    
    
    		/**  **/
    		public  static class Assetspec{
    			  private String alnvalue="";
    			  private String assetattrid="";
    			  private String linearassetspecid="";
    			  private String measureunitid;
    			  private List<Assetattribute> assetattribute;
    			  
    			  
    			public String getMeasureunitid() {
    				return measureunitid;
    			}
    
    			public void setMeasureunitid(String measureunitid) {
    				this.measureunitid = measureunitid;
    			}
    
    			public String getAlnvalue() {
    				return alnvalue;
    			}
    
    			public void setAlnvalue(String alnvalue) {
    				this.alnvalue = alnvalue.toUpperCase();
    			}
    
    			public String getAssetattrid() {
    				return assetattrid;
    			}
    
    			public void setAssetattrid(String assetattrid) {
    				this.assetattrid = assetattrid.toUpperCase();
    			}
    
    			public String getLinearassetspecid() {
    				return linearassetspecid;
    			}
    
    			public void setLinearassetspecid(String linearassetspecid) {
    				this.linearassetspecid = linearassetspecid.toUpperCase();
    			}
                
    
    			public List<Assetattribute> getAssetattribute() {
    				return assetattribute;
    			}
    
    			public void setAssetattribute(List<Assetattribute> assetattribute) {
    				this.assetattribute=JsonUtil.getDTOList(JSONArray.fromObject(assetattribute).toString(),Assetattribute.class);
    			}
    
    
    			/**  **/
    			public  static class Assetattribute{
    				  private String description="";
    				  private String orgid="";
    				public String getDescription() {
    					return description;
    				}
    				public void setDescription(String description) {
    					this.description = description.toUpperCase();
    				}
    				public String getOrgid() {
    					return orgid;
    				}
    				public void setOrgid(String orgid) {
    					this.orgid = orgid.toUpperCase();
    				}
    			
    				
    			  }
    		   }
    	   }
    	   
    	 }
     }
    }
    

     Json转实体的常用方法(工具类JsonUtil):

    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 net.sf.ezmorph.object.DateMorpher;
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    import net.sf.json.JsonConfig;
    import net.sf.json.util.JSONUtils;
    
    public class JsonUtil {      
         
        /**页面传至后台时,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    
         */     
        public static Object getDTO(String jsonString, Class clazz){      //这里仅用到了这一个方法  
            JSONObject jsonObject = null;      
            try{      
                setDataFormat2JAVA();       
                jsonObject = JSONObject.fromObject(jsonString);      
            }catch(Exception e){      
                e.printStackTrace();      
            }      
            return JSONObject.toBean(jsonObject, clazz);      
        }      
              
        /**    
         * 从一个JSON 对象字符格式中得到一个java对象,其中beansList是一类的集合,形如:    
         * {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...},    
         * beansList:[{}, {}, ...]}    
         * @param jsonString    
         * @param clazz    
         * @param map 集合属性的类型 (key : 集合属性名, value : 集合属性类型class) eg: ("beansList" : Bean.class)    
         * @return    
         */     
        public static Object getDTO(String jsonString, Class clazz, Map map){           
       JSONObject jsonObject = null; try{ setDataFormat2JAVA(); jsonObject = JSONObject.fromObject(jsonString); }catch(Exception e){ e.printStackTrace(); } return JSONObject.toBean(jsonObject, clazz, map); }
    /** * 从一个JSON数组得到一个java对象数组,形如: * [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...] * @param object * @param clazz * @return */ public static Object[] getDTOArray(String jsonString, Class clazz){ setDataFormat2JAVA(); JSONArray array = JSONArray.fromObject(jsonString); Object[] obj = new Object[array.size()]; for(int i = 0; i < array.size(); i++){ JSONObject jsonObject = array.getJSONObject(i); obj[i] = JSONObject.toBean(jsonObject, clazz); } return obj; } /** * 从一个JSON数组得到一个java对象数组,形如: * [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...] * @param object * @param clazz * @param map * @return */ public static Object[] getDTOArray(String jsonString, Class clazz, Map map){ setDataFormat2JAVA(); JSONArray array = JSONArray.fromObject(jsonString); Object[] obj = new Object[array.size()]; for(int i = 0; i < array.size(); i++){ JSONObject jsonObject = array.getJSONObject(i); obj[i] = JSONObject.toBean(jsonObject, clazz, map); } return obj; } /** * 从一个JSON数组得到一个java对象集合 * @param object * @param clazz * @return */ public static List getDTOList(String jsonString, Class clazz){ setDataFormat2JAVA(); 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; } /** * 从一个JSON数组得到一个java对象集合,其中对象中包含有集合属性 * @param object * @param clazz * @param map 集合属性的类型 * @return */ public static List getDTOList(String jsonString, Class clazz, Map map){ setDataFormat2JAVA(); 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, map)); } return list; } /** * 从json HASH表达式中获取一个map,该map支持嵌套功能 * 形如:{"id" : "johncon", "name" : "小强"} * 注意commons-collections版本,必须包含org.apache.commons.collections.map.MultiKeyMap * @param object * @return */ public static Map getMapFromJson(String jsonString) { setDataFormat2JAVA(); JSONObject jsonObject = JSONObject.fromObject(jsonString); Map map = new HashMap(); for(Iterator iter = jsonObject.keys(); iter.hasNext();){ String key = (String)iter.next(); map.put(key, jsonObject.get(key)); } return map; } /** * 从json数组中得到相应java数组 * json形如:["123", "456"] * @param jsonString * @return */ public static Object[] getObjectArrayFromJson(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); return jsonArray.toArray(); } /** * 把数据对象转换成json字符串 * DTO对象形如:{"id" : idValue, "name" : nameValue, ...} * 数组对象形如:[{}, {}, {}, ...] * map对象形如:{key1 : {"id" : idValue, "name" : nameValue, ...}, key2 : {}, ...} * @param object * @return */ /*public static String getJSONString(Object object) throws Exception{ String jsonString = null; //日期值处理器 JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor()); if(object != null){ if(object instanceof Collection || object instanceof Object[]){ jsonString = JSONArray.fromObject(object, jsonConfig).toString(); }else{ jsonString = JSONObject.fromObject(object, jsonConfig).toString(); } } return jsonString == null ? "{}" : jsonString; } */ private static void setDataFormat2JAVA(){ //设定日期转换格式 JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"})); } public static void main(String[] arg) throws Exception{ String s = "{status : 'success'}"; // System.out.println(" object : " + JsonUtil.getJSONString(s)); String mode1S="{id:'ada',name:'asdfa',addr:'asdfadfadsfadsf'}"; // Model1 model1=(Model1)JsonUtil.getDTO(mode1S, Model1.class); // System.out.println(model1.toString()); // // String model2S="{id:'ada',name:'asdfa',addr:'asdfadfadsfadsf',model3:{id:'12',name:'name',addr:'addr'}}"; // Model2 model2=(Model2)JsonUtil.getDTO(model2S, Model2.class); // System.out.println(model2.toString()); } }

    在测试类中,应该这样写:

      String equipJson = //Json字符串;

       equipJson=equipJson.toLowerCase();
       EquipRemoteData equipRemoteData=(EquipRemoteData) JsonUtil.getDTO(equipJson,new EquipRemoteData().getClass());
       List<Asset> lsAsset=equipRemoteData.getQuerymxassetdtsresponse().getMxassetdtsset().getAsset();

    到这里就算转换结束了。

    这里的所有代码都非我所写,是摘录别人的。


      

  • 相关阅读:
    Tomcat线程参数
    CDH平台规划注意事项
    python 不同数据类型的序列化
    Python 中__new__方法详解及使用
    线程生命周期
    如何在JAVA中每隔一段时间执行一段程序
    手动开启是事务提交回滚
    MySQL数据类型转换函数CAST与CONVERT的用法
    mybatis插入是返回主键id
    解决dubbo注册zookepper服务IP乱入问题的三种方式
  • 原文地址:https://www.cnblogs.com/qlong8807/p/2959149.html
Copyright © 2020-2023  润新知