Java将Json字符串转化为对象(包含list型变量)
来源:互联网 发布:hyde知乎 编辑:程序博客网 时间:2024/06/09 22:42
在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;}
0 0
- Java将Json字符串转化为对象(包含list型变量)
- 将json字符串转化为java对象
- Jackson将json字符串转化为泛型list
- 将json字符串转化为Map对象
- 如何将json对象转化为字符串?
- 将对象转化为JSON格式字符串
- Java如何将字符串转化为JSON(json-lib)
- JAVA 将json字符串转化为Map
- java 将复杂对象转化为Json
- Json转化为Java对象(object,List等)
- java对象转化为json字符串
- json-lib将json格式的字符串,转化为java对象
- java方式的将java对象以及list或者map转化为json数据
- java方式的将java对象以及list或者map转化为json数据 .
- java方式的将java对象以及list或者map转化为json数据 .
- 将一个字符串对象数组转化为List<对象>
- 使用Gson将List<T>()转化为json字符串
- Android--gson将list转化为json字符串
- iOS自动布局框架-Masonry详解
- centos 挂载与卸载硬盘
- 由Euler命名的数学符号和公式
- 《Cocoa设计模式》读书笔记(2)
- 【leetcode】115. Distinct Subsequences【java】
- Java将Json字符串转化为对象(包含list型变量)
- [leetcode]382. Linked List Random Node
- 字符串哈希函数
- 165. Compare Version Numbers
- 有关match_parent和wrap_content
- 《Cocoa设计模式》读书笔记(3)
- linux 定时器
- Oracle 修改用户密码
- 80211协议字段解析