版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Kevin_zhai/article/details/54286574
在Java中,经常会遇到要将前台传过来的Json字符串转化为对象。一些简单的对象类型,转化起来比较容易。但是,有时候也会遇到嵌套多层list的情况。这种情况,转化时就要麻烦一些。方法如下:
1.先建一个Map<String,Class> 类型的map,其中key为对象中list变量的名称,value是list变量类型的class类。
2.先把json字符串转化为jsonArray,然后遍历这个jsonArray,把每一个值都用toBean方法转化为指定的对象。
转化代码如下:
List<PromoteActivity> promoteActivities = new ArrayList<PromoteActivity>();
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("shares", ActivityCouponShare.class);
//转化活动详情数据
if(!StringUtils.isBlank(activityVOsStr)) {
JSONArray jsonArray = JSONArray.fromObject(activityVOsStr);
for(int i=0; i<jsonArray.size(); i++) {
JSONObject jsonObject = (JSONObject)jsonArray.get(i);
promoteActivities.add((PromoteActivity)JSONObject.toBean(jsonObject,PromoteActivity.class,classMap));
}
}
其中,classMap是用来存储PromoteActivity对象中list变量的键值对,把所有的list变量的键值对都放在里面即可。PromoteActivity的基本类代码如下:
public class PromoteActivity {
/**
* Table column t_carmktmis_promote_activity.id
*/
private Long id;
/**
* Table column t_carmktmis_promote_activity.promote_plan_id
*/
private Long promotePlanId;
/**
* Table column t_carmktmis_promote_activity.code
*/
private String code;
/**
* Table column t_carmktmis_promote_activity.popup_rate
*/
private Float popupRate;
/**
* Table column t_carmktmis_promote_activity.status
*/
private Integer status;
/**
* Table column t_carmktmis_promote_activity.share_logo_url
*/
private String shareLogoUrl;
/**
* Table column t_carmktmis_promote_activity.share_title
*/
private String shareTitle;
/**
* Table column t_carmktmis_promote_activity.share_summary
*/
private String shareSummary;
/**
* Table column t_carmktmis_promote_activity.share_o_content
*/
private String shareOContent;
/**
* Table column t_carmktmis_promote_activity.share_url
*/
private String shareUrl;
/**
* Table column t_carmktmis_promote_activity.update_uid
*/
private String updateUid;
/**
* Table column t_carmktmis_promote_activity.update_time
*/
private Date updateTime;
/**
* Table column t_carmktmis_promote_activity.remark
*/
private String remark;
/**
* 分享话术列表
*/
private List<ActivityCouponShare> shares;
/**
* @see PromoteActivity#id
*/
public Long getId() {
return id;
}
/**
* @see PromoteActivity#id
*/
public void setId(Long id) {
this.id = id;
}
/**
* @see PromoteActivity#promotePlanId
*/
public Long getPromotePlanId() {
return promotePlanId;
}
/**
* @see PromoteActivity#promotePlanId
*/
public void setPromotePlanId(Long promotePlanId) {
this.promotePlanId = promotePlanId;
}
/**
* @see PromoteActivity#code
*/
public String getCode() {
return code;
}
/**
* @see PromoteActivity#code
*/
public void setCode(String code) {
this.code = code;
}
/**
* @see PromoteActivity#popupRate
*/
public Float getPopupRate() {
return popupRate;
}
/**
* @see PromoteActivity#popupRate
*/
public void setPopupRate(Float popupRate) {
this.popupRate = popupRate;
}
/**
* @see PromoteActivity#status
*/
public Integer getStatus() {
return status;
}
/**
* @see PromoteActivity#status
*/
public void setStatus(Integer status) {
this.status = status;
}
/**
* @see PromoteActivity#shareLogoUrl
*/
public String getShareLogoUrl() {
return shareLogoUrl;
}
/**
* @see PromoteActivity#shareLogoUrl
*/
public void setShareLogoUrl(String shareLogoUrl) {
this.shareLogoUrl = shareLogoUrl;
}
/**
* @see PromoteActivity#shareTitle
*/
public String getShareTitle() {
return shareTitle;
}
/**
* @see PromoteActivity#shareTitle
*/
public void setShareTitle(String shareTitle) {
this.shareTitle = shareTitle;
}
/**
* @see PromoteActivity#shareSummary
*/
public String getShareSummary() {
return shareSummary;
}
/**
* @see PromoteActivity#shareSummary
*/
public void setShareSummary(String shareSummary) {
this.shareSummary = shareSummary;
}
/**
* @see PromoteActivity#shareOContent
*/
public String getShareOContent() {
return shareOContent;
}
/**
* @see PromoteActivity#shareOContent
*/
public void setShareOContent(String shareOContent) {
this.shareOContent = shareOContent;
}
/**
* @see PromoteActivity#shareUrl
*/
public String getShareUrl() {
return shareUrl;
}
/**
* @see PromoteActivity#shareUrl
*/
public void setShareUrl(String shareUrl) {
this.shareUrl = shareUrl;
}
/**
* @see PromoteActivity#updateUid
*/
public String getUpdateUid() {
return updateUid;
}
/**
* @see PromoteActivity#updateUid
*/
public void setUpdateUid(String updateUid) {
this.updateUid = updateUid;
}
/**
* @see PromoteActivity#updateTime
*/
public Date getUpdateTime() {
return updateTime;
}
/**
* @see PromoteActivity#updateTime
*/
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
/**
* @see PromoteActivity#remark
*/
public String getRemark() {
return remark;
}
/**
* @see PromoteActivity#remark
*/
public void setRemark(String remark) {
this.remark = remark;
}
public List<ActivityCouponShare> getShares() {
return shares;
}
public void setShares(List<ActivityCouponShare> shares) {
this.shares = shares;
}
}
ActivityCouponShare基本类型的代码如下
public class ActivityCouponShare {
/**
* Table column t_carmktmis_activity_coupon_share.id
*/
private Long id;
/**
* Table column t_carmktmis_activity_coupon_share.promote_activity_id
*/
private Long promoteActivityId;
/**
* Table column t_carmktmis_activity_coupon_share.coupon_cate_id
*/
private Long couponCateId;
/**
* Table column t_carmktmis_activity_coupon_share.share_content
*/
private String shareContent;
/**
* 代金券面值
*/
private Integer couponValue;
/**
* @see ActivityCouponShare#id
*/
public Long getId() {
return id;
}
/**
* @see ActivityCouponShare#id
*/
public void setId(Long id) {
this.id = id;
}
/**
* @see ActivityCouponShare#promoteActivityId
*/
public Long getPromoteActivityId() {
return promoteActivityId;
}
/**
* @see ActivityCouponShare#promoteActivityId
*/
public void setPromoteActivityId(Long promoteActivityId) {
this.promoteActivityId = promoteActivityId;
}
/**
* @see ActivityCouponShare#couponCateId
*/
public Long getCouponCateId() {
return couponCateId;
}
/**
* @see ActivityCouponShare#couponCateId
*/
public void setCouponCateId(Long couponCateId) {
this.couponCateId = couponCateId;
}
/**
* @see ActivityCouponShare#shareContent
*/
public String getShareContent() {
return shareContent;
}
/**
* @see ActivityCouponShare#shareContent
*/
public void setShareContent(String shareContent) {
this.shareContent = shareContent;
}
public Integer getCouponValue() {
return couponValue;
}
public void setCouponValue(Integer couponValue) {
this.couponValue = couponValue;
}