Map转换为bean
Map转换为bean,Map里可以包含List,需要的包commons-beanutils.jar
1 转换类(DefaultResult)
public void fromMap(Map<String, Object> map,String datePattern,Map<String, Class> type) { BeanMap beanMap = new BeanMap(this); for(Object obj : beanMap.keySet()){ if(map.containsKey(obj.toString())){ Object value = map.get(obj.toString()); if(value instanceof Date){ beanMap.put(obj, DateFormatUtils.format((Date)value, datePattern)); }if(value instanceof List){ List toList = new ArrayList(); List list= (List)value; for(Object subObj : list){ try { Object temp = (type.get(obj.toString()).newInstance()); DefaultResult result = (DefaultResult)temp; result.fromBean(subObj); toList.add(result); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } beanMap.put(obj, toList); }else{ beanMap.put(obj, value); } } } } public void fromMap(Map<String, Object> map){ fromMap(map,defaultDateFromatPattern); } public void fromMap(Map<String, Object> map,String datePattern){ fromMap(map, datePattern, null); } public void fromBean(Object obj){ try { BeanUtils.copyProperties(this, obj); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }
2 需要转换的VO
public class GameWholelistResult extends DefaultResult{ private boolean hasnext; private Integer total; private List<GameListResult> list; private List<BannerListResult> blist; public boolean isHasnext() { return hasnext; } public void setHasnext(boolean hasnext) { this.hasnext = hasnext; } public Integer getTotal() { return total; } public void setTotal(Integer total) { this.total = total; } public List<GameListResult> getList() { return list; } public void setList(List<GameListResult> list) { this.list = list; } public List<BannerListResult> getBlist() { return blist; } public void setBlist(List<BannerListResult> blist) { this.blist = blist; } public static class GameListResult extends DefaultResult{ private String id; private String name; private String logo; private String createtime; private String typeid; private String typename; private Integer appsize; private Integer apprank; private String ages; private String childtype; private Integer downloadcount; private Integer commentcount; private String version; private String pkg; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLogo() { return logo; } public void setLogo(String logo) { this.logo = logo; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getTypeid() { return typeid; } public void setTypeid(String typeid) { this.typeid = typeid; } public String getTypename() { return typename; } public void setTypename(String typename) { this.typename = typename; } public Integer getAppsize() { return appsize; } public void setAppsize(Integer appsize) { this.appsize = appsize; } public Integer getApprank() { return apprank; } public void setApprank(Integer apprank) { this.apprank = apprank; } public String getAges() { return ages; } public void setAges(String ages) { this.ages = ages; } public String getChildtype() { return childtype; } public void setChildtype(String childtype) { this.childtype = childtype; } public Integer getDownloadcount() { return downloadcount; } public void setDownloadcount(Integer downloadcount) { this.downloadcount = downloadcount; } public Integer getCommentcount() { return commentcount; } public void setCommentcount(Integer commentcount) { this.commentcount = commentcount; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public String getPkg() { return pkg; } public void setPkg(String pkg) { this.pkg = pkg; } @Override @JsonIgnore public Integer getStatus() { return super.getStatus(); } @Override @JsonIgnore public String getErrormessage() { return super.getErrormessage(); } } public static class BannerListResult extends DefaultResult{ private String image; private Integer locationtype; private String locationid; public String getImage() { return image; } public void setImage(String image) { this.image = image; } public Integer getLocationtype() { return locationtype; } public void setLocationtype(Integer locationtype) { this.locationtype = locationtype; } public String getLocationid() { return locationid; } public void setLocationid(String locationid) { this.locationid = locationid; } public boolean ignoreDefault(){ return true; } @Override @JsonIgnore public Integer getStatus() { return super.getStatus(); } @Override @JsonIgnore public String getErrormessage() { return super.getErrormessage(); } } /*public GameListResult getNewGameListResult(){ return new GameListResult(); } public BannerListResult getNewBannerListResult(){ return new BannerListResult(); }*/ }
3 实现用法
Map<String , Class> classType= new HashMap<String, Class>();
classType.put("blist", BannerListResult.class); classType.put("list", GameListResult.class); GameWholelistResult gameWholelistResult = new GameWholelistResult(); ameWholelistResult.fromMap(resultMap,"yyyy-MM-dd",classType);
具体的实现,大家可以看看源码,能够更清晰原理