json字符串转化成含多层list集合的对象

本文介绍如何解析订单数据报文并将其转换为Java实体类,包括DeliveryInfoDto、DataInfoDto、GoodsInfoDto及ExpressInfoDto等类的设计与使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

返回待解析报文  

[java]   view plain  copy
  1. "DATA" : [ {  
  2.     "infoSource" : "IN",  
  3.     "actOrderId" : 110718608,  
  4.     "orderAppId" : "3200130001",  
  5.     "orderAppCustomCategory" : null,  
  6.     "channelLevel1" : 0,  
  7.     "channelLevel2" : 0,  
  8.     "channelLevel3" : 0,  
  9.     "channelLevel4" : null,  
  10.     "orderStatus" : 2,  
  11.     "orderStatusDesc" : "已支付(待发货) ",  
  12.     "payStatus" : 2,  
  13.     "refundStatus" : 0,  
  14.     "expressStatus" : 2,  
  15.     "clossStatus" : 0,  
  16.     "createTime" : "20151028101923",  
  17.     "payTime" : "20151028102000",  
  18.     "userId" : 177383800,  
  19.     "andUserId" : null,  
  20.     "showUrl" : null,  
  21.     "shopId" : "11224193",  
  22.     "shopName" : "避风塘",  
  23.     "hasRefund" : 0,  
  24.     "payOnDelivery" : 0,  
  25.     "goodsInfos" : [ {  
  26.       "goodsId" : 78259824,  
  27.       "goodsName" : "食堂活动-自提商品",  
  28.       "goodsImage" : "p_1911291824.jpg",  
  29.       "price" : 1,  
  30.       "count" : 3,  
  31.       "goodsDetail" : null,  
  32.       "goodsDesc" : null,  
  33.       "isVirtual" : 0,  
  34.       "payCount" : 3,  
  35.       "goodsVersion" : "40170570"  
  36.     } ],  
  37.     "expressInfos" : [ {  
  38.       "expressCompanyId" : 10000,  
  39.       "expressNo" : null,  
  40.       "receiverName" : null,  
  41.       "address" : null,  
  42.       "phoneNumber" : null,  
  43.       "receiveTime" : "20151028103413",  
  44.       "expressCost" : 0,  
  45.       "status" : 2  
  46.     } ],  


代码转化示例:

[java]   view plain  copy
  1. // JSON转换    
  2.         JSONObject jsonObj = JSONObject.fromObject(goodsJson);    
  3.         Map<String, Class> classMap = new HashMap<String, Class>();    
  4.         classMap.put("DATA", DataInfoDto.class);    
  5.         classMap.put("goodsInfos", GoodsInfoDto.class);      
  6.         classMap.put("expressInfos", ExpressInfoDto.class);      
  7.         // 将JSON转换成DeliveryInfoDto      
  8.         DeliveryInfoDto delivery = (DeliveryInfoDto) JSONObject.toBean(jsonObj,      
  9.         DeliveryInfoDto.class, classMap);    

创建相关实体类:

[html]   view plain  copy
  1. package com.justinmobile.lticket.domain.dto;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.List;  
  5.   
  6. /**  
  7.  * 返回提货订单信息  
  8.  * @author cm  
  9.  */  
  10. @SuppressWarnings("serial")  
  11. public class DeliveryInfoDto implements Serializable  
  12. {  
  13.     public String TOTALROW;  
  14.     public List<DataInfoDto> DATA;  
  15.       
  16.     public DeliveryInfoDto(){  
  17.         super();  
  18.     }  
  19.   
  20.     public String getTOTALROW()  
  21.     {  
  22.         return TOTALROW;  
  23.     }  
  24.   
  25.     public void setTOTALROW(String tOTALROW)  
  26.     {  
  27.         TOTALROW = tOTALROW;  
  28.     }  
  29.   
  30.     public List<DataInfoDto> getDATA()  
  31.     {  
  32.         return DATA;  
  33.     }  
  34.   
  35.     public void setDATA(List<DataInfoDto> dATA)  
  36.     {  
  37.         DATA = dATA;  
  38.     }  
  39.       
  40. }  



[java]   view plain  copy
  1. package com.justinmobile.lticket.domain.dto;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.List;  
  5.   
  6. /** 
  7.  * 返回提货订单信息 
  8.  * @author cm 
  9.  */  
  10. @SuppressWarnings("serial")  
  11. public class DataInfoDto implements Serializable  
  12. {  
  13.     public String infoSource;  
  14.     public Long actOrderId;  
  15.     public String orderAppId;  
  16.     public String orderAppCustomCategory;  
  17.     public int channelLevel1;  
  18.     public int channelLevel2;  
  19.     public int channelLevel3;  
  20.     public String channelLevel4;  
  21.     public int orderStatus;  
  22.     public String orderStatusDesc;  
  23.     public int payStatus;  
  24.     public int refundStatus;  
  25.     public int expressStatus;  
  26.     public int clossStatus;  
  27.     public String createTime;  
  28.     public String payTime;  
  29.     public Long userId;  
  30.     public String andUserId;  
  31.     public String showUrl;    
  32.     public String shopId;  
  33.     public String shopName;  
  34.     public int hasRefund;  
  35.     public int payOnDelivery;  
  36.     public List<GoodsInfoDto> goodsInfos;  
  37.     public List<ExpressInfoDto> expressInfos;  
  38.     public int totalPay;  
  39.     public String refundUrl;  
  40.       
  41.     public DataInfoDto(){  
  42.         super();  
  43.     }  
  44.     public String getInfoSource()  
  45.     {  
  46.         return infoSource;  
  47.     }  
  48.     public void setInfoSource(String infoSource)  
  49.     {  
  50.         this.infoSource = infoSource;  
  51.     }  
  52.     public Long getActOrderId()  
  53.     {  
  54.         return actOrderId;  
  55.     }  
  56.     public void setActOrderId(Long actOrderId)  
  57.     {  
  58.         this.actOrderId = actOrderId;  
  59.     }  
  60.     public String getOrderAppId()  
  61.     {  
  62.         return orderAppId;  
  63.     }  
  64.     public void setOrderAppId(String orderAppId)  
  65.     {  
  66.         this.orderAppId = orderAppId;  
  67.     }  
  68.     public String getOrderAppCustomCategory()  
  69.     {  
  70.         return orderAppCustomCategory;  
  71.     }  
  72.     public void setOrderAppCustomCategory(String orderAppCustomCategory)  
  73.     {  
  74.         this.orderAppCustomCategory = orderAppCustomCategory;  
  75.     }  
  76.     public int getChannelLevel1()  
  77.     {  
  78.         return channelLevel1;  
  79.     }  
  80.     public void setChannelLevel1(int channelLevel1)  
  81.     {  
  82.         this.channelLevel1 = channelLevel1;  
  83.     }  
  84.     public int getChannelLevel2()  
  85.     {  
  86.         return channelLevel2;  
  87.     }  
  88.     public void setChannelLevel2(int channelLevel2)  
  89.     {  
  90.         this.channelLevel2 = channelLevel2;  
  91.     }  
  92.     public int getChannelLevel3()  
  93.     {  
  94.         return channelLevel3;  
  95.     }  
  96.     public void setChannelLevel3(int channelLevel3)  
  97.     {  
  98.         this.channelLevel3 = channelLevel3;  
  99.     }  
  100.     public String getChannelLevel4()  
  101.     {  
  102.         return channelLevel4;  
  103.     }  
  104.     public void setChannelLevel4(String channelLevel4)  
  105.     {  
  106.         this.channelLevel4 = channelLevel4;  
  107.     }  
  108.     public int getOrderStatus()  
  109.     {  
  110.         return orderStatus;  
  111.     }  
  112.     public void setOrderStatus(int orderStatus)  
  113.     {  
  114.         this.orderStatus = orderStatus;  
  115.     }  
  116.     public String getOrderStatusDesc()  
  117.     {  
  118.         return orderStatusDesc;  
  119.     }  
  120.     public void setOrderStatusDesc(String orderStatusDesc)  
  121.     {  
  122.         this.orderStatusDesc = orderStatusDesc;  
  123.     }  
  124.     public int getPayStatus()  
  125.     {  
  126.         return payStatus;  
  127.     }  
  128.     public void setPayStatus(int payStatus)  
  129.     {  
  130.         this.payStatus = payStatus;  
  131.     }  
  132.     public int getRefundStatus()  
  133.     {  
  134.         return refundStatus;  
  135.     }  
  136.     public void setRefundStatus(int refundStatus)  
  137.     {  
  138.         this.refundStatus = refundStatus;  
  139.     }  
  140.     public int getExpressStatus()  
  141.     {  
  142.         return expressStatus;  
  143.     }  
  144.     public void setExpressStatus(int expressStatus)  
  145.     {  
  146.         this.expressStatus = expressStatus;  
  147.     }  
  148.     public int getClossStatus()  
  149.     {  
  150.         return clossStatus;  
  151.     }  
  152.     public void setClossStatus(int clossStatus)  
  153.     {  
  154.         this.clossStatus = clossStatus;  
  155.     }  
  156.     public String getCreateTime()  
  157.     {  
  158.         return createTime;  
  159.     }  
  160.     public void setCreateTime(String createTime)  
  161.     {  
  162.         this.createTime = createTime;  
  163.     }  
  164.     public String getPayTime()  
  165.     {  
  166.         return payTime;  
  167.     }  
  168.     public void setPayTime(String payTime)  
  169.     {  
  170.         this.payTime = payTime;  
  171.     }  
  172.     public Long getUserId()  
  173.     {  
  174.         return userId;  
  175.     }  
  176.     public void setUserId(Long userId)  
  177.     {  
  178.         this.userId = userId;  
  179.     }  
  180.     public String getAndUserId()  
  181.     {  
  182.         return andUserId;  
  183.     }  
  184.     public void setAndUserId(String andUserId)  
  185.     {  
  186.         this.andUserId = andUserId;  
  187.     }  
  188.     public String getShowUrl()  
  189.     {  
  190.         return showUrl;  
  191.     }  
  192.     public void setShowUrl(String showUrl)  
  193.     {  
  194.         this.showUrl = showUrl;  
  195.     }  
  196.     public String getShopId()  
  197.     {  
  198.         return shopId;  
  199.     }  
  200.     public void setShopId(String shopId)  
  201.     {  
  202.         this.shopId = shopId;  
  203.     }  
  204.     public String getShopName()  
  205.     {  
  206.         return shopName;  
  207.     }  
  208.     public void setShopName(String shopName)  
  209.     {  
  210.         this.shopName = shopName;  
  211.     }  
  212.     public int getHasRefund()  
  213.     {  
  214.         return hasRefund;  
  215.     }  
  216.     public void setHasRefund(int hasRefund)  
  217.     {  
  218.         this.hasRefund = hasRefund;  
  219.     }  
  220.     public int getPayOnDelivery()  
  221.     {  
  222.         return payOnDelivery;  
  223.     }  
  224.     public void setPayOnDelivery(int payOnDelivery)  
  225.     {  
  226.         this.payOnDelivery = payOnDelivery;  
  227.     }  
  228.     public List<GoodsInfoDto> getGoodsInfos()  
  229.     {  
  230.         return goodsInfos;  
  231.     }  
  232.     public void setGoodsInfos(List<GoodsInfoDto> goodsInfos)  
  233.     {  
  234.         this.goodsInfos = goodsInfos;  
  235.     }  
  236.     public List<ExpressInfoDto> getExpressInfos()  
  237.     {  
  238.         return expressInfos;  
  239.     }  
  240.     public void setExpressInfos(List<ExpressInfoDto> expressInfos)  
  241.     {  
  242.         this.expressInfos = expressInfos;  
  243.     }  
  244.     public int getTotalPay()  
  245.     {  
  246.         return totalPay;  
  247.     }  
  248.     public void setTotalPay(int totalPay)  
  249.     {  
  250.         this.totalPay = totalPay;  
  251.     }  
  252.     public String getRefundUrl()  
  253.     {  
  254.         return refundUrl;  
  255.     }  
  256.     public void setRefundUrl(String refundUrl)  
  257.     {  
  258.         this.refundUrl = refundUrl;  
  259.     }  
  260.   
  261. }  

[html]   view plain  copy
  1. package com.justinmobile.lticket.domain.dto;  
  2.   
  3. import java.io.Serializable;  
  4. import java.math.BigDecimal;  
  5.   
  6. /**  
  7.  * 商品信息DTO  
  8.  * @author cm  
  9.  *  
  10.  */  
  11. @SuppressWarnings("serial")  
  12. public class GoodsInfoDto implements Serializable  
  13. {  
  14.     public Long goodsId;  
  15.     public String goodsName;  
  16.     public String goodsImage;  
  17.     public BigDecimal price;  
  18.     public int count;  
  19.     public String goodsDetail;  
  20.     public String goodsDesc;  
  21.     public int isVirtual;  
  22.     public int payCount;  
  23.     public String goodsVersion;  
  24.       
  25.     public GoodsInfoDto(){  
  26.         super();  
  27.     }  
  28.     public Long getGoodsId()  
  29.     {  
  30.         return goodsId;  
  31.     }  
  32.     public void setGoodsId(Long goodsId)  
  33.     {  
  34.         this.goodsId = goodsId;  
  35.     }  
  36.     public String getGoodsName()  
  37.     {  
  38.         return goodsName;  
  39.     }  
  40.     public void setGoodsName(String goodsName)  
  41.     {  
  42.         this.goodsName = goodsName;  
  43.     }  
  44.     public String getGoodsImage()  
  45.     {  
  46.         return goodsImage;  
  47.     }  
  48.     public void setGoodsImage(String goodsImage)  
  49.     {  
  50.         this.goodsImage = goodsImage;  
  51.     }  
  52.     public BigDecimal getPrice()  
  53.     {  
  54.         return price;  
  55.     }  
  56.     public void setPrice(BigDecimal price)  
  57.     {  
  58.         this.price = price;  
  59.     }  
  60.     public int getCount()  
  61.     {  
  62.         return count;  
  63.     }  
  64.     public void setCount(int count)  
  65.     {  
  66.         this.count = count;  
  67.     }  
  68.     public String getGoodsDetail()  
  69.     {  
  70.         return goodsDetail;  
  71.     }  
  72.     public void setGoodsDetail(String goodsDetail)  
  73.     {  
  74.         this.goodsDetail = goodsDetail;  
  75.     }  
  76.     public String getGoodsDesc()  
  77.     {  
  78.         return goodsDesc;  
  79.     }  
  80.     public void setGoodsDesc(String goodsDesc)  
  81.     {  
  82.         this.goodsDesc = goodsDesc;  
  83.     }  
  84.     public int getIsVirtual()  
  85.     {  
  86.         return isVirtual;  
  87.     }  
  88.     public void setIsVirtual(int isVirtual)  
  89.     {  
  90.         this.isVirtual = isVirtual;  
  91.     }  
  92.     public int getPayCount()  
  93.     {  
  94.         return payCount;  
  95.     }  
  96.     public void setPayCount(int payCount)  
  97.     {  
  98.         this.payCount = payCount;  
  99.     }  
  100.     public String getGoodsVersion()  
  101.     {  
  102.         return goodsVersion;  
  103.     }  
  104.     public void setGoodsVersion(String goodsVersion)  
  105.     {  
  106.         this.goodsVersion = goodsVersion;  
  107.     }  
  108.   
  109. }  

[html]   view plain  copy
  1. package com.justinmobile.lticket.domain.dto;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. /**  
  6.  * 物流信息DTO  
  7.  * @author cm  
  8.  *  
  9.  */  
  10. @SuppressWarnings("serial")  
  11. public class ExpressInfoDto implements Serializable  
  12. {  
  13.     public Long expressCompanyId;  
  14.     public String expressNo;  
  15.     public String receiverName;  
  16.     public String address;  
  17.     public String phoneNumber;  
  18.     public String receiveTime;  
  19.     public int expressCost;  
  20.     public int status;  
  21.       
  22.     public ExpressInfoDto(){  
  23.         super();  
  24.     }  
  25.     public Long getExpressCompanyId()  
  26.     {  
  27.         return expressCompanyId;  
  28.     }  
  29.     public void setExpressCompanyId(Long expressCompanyId)  
  30.     {  
  31.         this.expressCompanyId = expressCompanyId;  
  32.     }  
  33.     public String getExpressNo()  
  34.     {  
  35.         return expressNo;  
  36.     }  
  37.     public void setExpressNo(String expressNo)  
  38.     {  
  39.         this.expressNo = expressNo;  
  40.     }  
  41.     public String getReceiverName()  
  42.     {  
  43.         return receiverName;  
  44.     }  
  45.     public void setReceiverName(String receiverName)  
  46.     {  
  47.         this.receiverName = receiverName;  
  48.     }  
  49.     public String getPhoneNumber()  
  50.     {  
  51.         return phoneNumber;  
  52.     }  
  53.     public void setPhoneNumber(String phoneNumber)  
  54.     {  
  55.         this.phoneNumber = phoneNumber;  
  56.     }  
  57.     public String getReceiveTime()  
  58.     {  
  59.         return receiveTime;  
  60.     }  
  61.     public void setReceiveTime(String receiveTime)  
  62.     {  
  63.         this.receiveTime = receiveTime;  
  64.     }  
  65.     public int getExpressCost()  
  66.     {  
  67.         return expressCost;  
  68.     }  
  69.     public void setExpressCost(int expressCost)  
  70.     {  
  71.         this.expressCost = expressCost;  
  72.     }  
  73.     public int getStatus()  
  74.     {  
  75.         return status;  
  76.     }  
  77.     public void setStatus(int status)  
  78.     {  
  79.         this.status = status;  
  80.     }  
  81.     public String getAddress()  
  82.     {  
  83.         return address;  
  84.     }  
  85.     public void setAddress(String address)  
  86.     {  
  87.         this.address = address;  
  88.     }  
  89.     
  90.       
  91. }  

<->.DeliveryInfoDto 类的属性是 json的字符串 key, DATA.类型是一个最外层的列表对象。List<DataInfoDto>

<二>.里面的json key .跟对象类名相同


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值