[JAVA] Java从json提取数组并转换为list的操作方法

1837 0
黑夜隐士 2022-11-9 09:41:57 | 显示全部楼层 |阅读模式
目录

    Java 从json提取数组并转换为listJava单个对象和List对象转换成Json,Json转List
      (一)使用单个对象转换JSON对象(二)多个对象存到List,再转换成JSON(三)json的list对象转List对象



Java 从json提取数组并转换为list

这里ret表示json字符串原文
  1. // 解析为JSONObject
  2. JSONObject jsonObject = JSONObject.parseObject(ret);
  3. // 提取出JSONArray
  4. JSONArray jsonArray = new JSONArray(jsonObject.getJSONObject("result").getJSONArray("org_list"));
  5. // 将JSONArray转为List列表
  6. String str = JSONObject.toJSONString(jsonArray);
  7. List<Org> list = JSONObject.parseArray(str, Org.class);
复制代码
使用getJSONArray()获取到jsonarray后,再将jsonArray转换为字符串,最后将字符串解析为List列表。
代码中的Org是我List列表中元素的类型。

Java单个对象和List对象转换成Json,Json转List


(一)使用单个对象转换JSON对象

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.junit.Test; import com.css.eshop.exception.DataAccessException;import com.css.eshop.model.VoucherInfo;import com.css.eshop.util.HttpClientUtil;import com.css.eshop.util.LoadStaticReferenceTables; import net.sf.json.JSONArray;import net.sf.json.JSONObject; public class TestPut {        protected Log logger = LogFactory.getLog(this.getClass().getName());                @Test        public void getOneJson(){//测试转换成Json,单个对象                VoucherInfo vo1=new VoucherInfo();                vo1.setVoucherValue(2131);vo1.setVoucherCode("小可爱的");                JSONObject updateJsonObj = JSONObject.fromObject(vo1);//转换成json格式                logger.info("---转换成json格式:---"+updateJsonObj.toString());//提取access_token节点数据        }}
输出转换后日志:


17-10-2018 17:35 INFO  TestPut:35 - ---转换成json格式:---{"labourCost":0,"refMetalPrice":0,"voucherCoNumber":"","voucherCode":"小可爱的","voucherType":"","voucherValue":2131,"weight":0,"weightUnit":""}

(二)多个对象存到List,再转换成JSON
  1.         @Test
  2.         public void getArrayList(){//测试转换成Json,List转换成JSONList
  3.                 List<VoucherInfo> vouchersList=new ArrayList<VoucherInfo>();
  4.                 VoucherInfo vo1=new VoucherInfo();
  5.                 VoucherInfo vo2=new VoucherInfo();
  6.                 vo1.setVoucherValue(2131);vo1.setVoucherCode("小可爱的");
  7.                 vo2.setVoucherValue(100);vo2.setVoucherCode("小可爱的222");
  8.                 vouchersList.add(vo1);vouchersList.add(vo2);
  9.                 JSONArray jsonArray = JSONArray.fromObject(vouchersList);
  10.                 logger.info("--获取到转换为json格式的内容:"+jsonArray.toString());//提取access_token节点数据
  11.         }
复制代码
输出日志:


17-10-2018 17:36 INFO  TestPut:47 - --获取到转换为json格式的内容:[{"labourCost":0,"refMetalPrice":0,"voucherCoNumber":"","voucherCode":"小可爱的","voucherType":"","voucherValue":2131,"weight":0,"weightUnit":""},{"labourCost":0,"refMetalPrice":0,"voucherCoNumber":"","voucherCode":"小可爱的222","voucherType":"","voucherValue":100,"weight":0,"weightUnit":""}]

(三)json的list对象转List对象

先转jsonArray,再转object提取数据
  1.         @Test
  2.         public void test1210() {
  3.                 List<VoucherInfo> vouchers=new ArrayList<VoucherInfo>();
  4.                 VoucherInfo voucherInfo1=new VoucherInfo();VoucherInfo voucherInfo2=new VoucherInfo();
  5.                 voucherInfo1.setVoucherCode("coupncdeC0000003");voucherInfo1.setVoucherType("DISC_VOUCHER");
  6.                 voucherInfo1.setVoucherCoNumber(null);voucherInfo1.setLabourCost(0.0);voucherInfo1.setWeight(0.0);
  7.                 voucherInfo1.setVoucherValue(1000.0);voucherInfo1.setRefMetalPrice(0.0);
  8.                 voucherInfo2.setVoucherCode("coupncdeC0000001");voucherInfo2.setVoucherType("DISC_VOUCHER");
  9.                 voucherInfo2.setVoucherCoNumber(null);voucherInfo2.setLabourCost(0.0);voucherInfo2.setWeight(0.0);
  10.                 voucherInfo2.setVoucherValue(1000.0);voucherInfo2.setRefMetalPrice(0.0);
  11.                 vouchers.add(voucherInfo1);vouchers.add(voucherInfo2);
  12.                 double ecoupon_amt=0;
  13.                 if(null != vouchers  && vouchers.size()>0){
  14.                         JSONArray vouchersListJsonArray = JSONArray.fromObject(vouchers);
  15.                         String vouchersListJson=vouchersListJsonArray.toString();
  16.                         System.out.println(vouchersListJson);
  17.                         if(null != vouchersListJson){
  18.                                 JSONArray jsonArray1 = JSONArray.fromObject(vouchersListJson);
  19.                                 //循环获取json数组中的 json 对象,然后转换为 object
  20.                         for (int j = 0; j < jsonArray1.size(); j++) {  
  21.                             JSONObject jsonObject2 = jsonArray1.getJSONObject(j);  
  22.                             VoucherInfo cust = (VoucherInfo) JSONObject.toBean(jsonObject2, VoucherInfo.class);  
  23.                             ecoupon_amt=ecoupon_amt+cust.getVoucherValue();
  24.                         }  
  25.                         }
  26.                 }
  27.                 System.out.println(ecoupon_amt);
  28.         }
复制代码
到此这篇关于Java 从json提取出数组并转换为list的文章就介绍到这了,更多相关java json提取数组转换为list内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行