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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

滕青山YYDS   2022-11-02 我要评论

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

这里ret表示json字符串原文

// 解析为JSONObject 
JSONObject jsonObject = JSONObject.parseObject(ret);
// 提取出JSONArray 
JSONArray jsonArray = new JSONArray(jsonObject.getJSONObject("result").getJSONArray("org_list"));
// 将JSONArray转为List列表
String str = JSONObject.toJSONString(jsonArray);
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

	@Test
	public void getArrayList(){//测试转换成Json,List转换成JSONList
		List<VoucherInfo> vouchersList=new ArrayList<VoucherInfo>();
		VoucherInfo vo1=new VoucherInfo();
		VoucherInfo vo2=new VoucherInfo();
		vo1.setVoucherValue(2131);vo1.setVoucherCode("小可爱的");
		vo2.setVoucherValue(100);vo2.setVoucherCode("小可爱的222");
		vouchersList.add(vo1);vouchersList.add(vo2);
		JSONArray jsonArray = JSONArray.fromObject(vouchersList);
		logger.info("--获取到转换为json格式的内容:"+jsonArray.toString());//提取access_token节点数据
		
	}

输出日志:

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提取数据

	@Test
	public void test1210() {
		List<VoucherInfo> vouchers=new ArrayList<VoucherInfo>();
		VoucherInfo voucherInfo1=new VoucherInfo();VoucherInfo voucherInfo2=new VoucherInfo();
		voucherInfo1.setVoucherCode("coupncdeC0000003");voucherInfo1.setVoucherType("DISC_VOUCHER");
		voucherInfo1.setVoucherCoNumber(null);voucherInfo1.setLabourCost(0.0);voucherInfo1.setWeight(0.0);
		voucherInfo1.setVoucherValue(1000.0);voucherInfo1.setRefMetalPrice(0.0);
		
		voucherInfo2.setVoucherCode("coupncdeC0000001");voucherInfo2.setVoucherType("DISC_VOUCHER");
		voucherInfo2.setVoucherCoNumber(null);voucherInfo2.setLabourCost(0.0);voucherInfo2.setWeight(0.0);
		voucherInfo2.setVoucherValue(1000.0);voucherInfo2.setRefMetalPrice(0.0);
		vouchers.add(voucherInfo1);vouchers.add(voucherInfo2);
		double ecoupon_amt=0;
		if(null != vouchers  && vouchers.size()>0){
			JSONArray vouchersListJsonArray = JSONArray.fromObject(vouchers);
			String vouchersListJson=vouchersListJsonArray.toString();
			
			System.out.println(vouchersListJson);
			if(null != vouchersListJson){
				JSONArray jsonArray1 = JSONArray.fromObject(vouchersListJson);
				//循环获取json数组中的 json 对象,然后转换为 object
		        for (int j = 0; j < jsonArray1.size(); j++) {  
		            JSONObject jsonObject2 = jsonArray1.getJSONObject(j);  
		            VoucherInfo cust = (VoucherInfo) JSONObject.toBean(jsonObject2, VoucherInfo.class);  
		            ecoupon_amt=ecoupon_amt+cust.getVoucherValue();
		        }  
			}
		}
		System.out.println(ecoupon_amt);
	}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们