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

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

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);
	}

到此这篇关于Java 从json提取出数组并转换为list的文章就介绍到这了,更多相关java json提取数组转换为list内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • java提取json中某个数组的所有值方法

    实例如下所示: public class JsonExtracter { public static void main(String[] args) { String s = "{\"name\":\"a\",\"family\":[\"张三\",\"李四\"]}"; JSONObject jsonObject = JSON.parseObject(s); //注意:family中的内

  • Java中Json字符串直接转换为对象的方法(包括多层List集合)

    使用到的类:net.sf.json.JSONObject 使用JSON时,除了要导入JSON网站上面下载的json-lib-2.2-jdk15.jar包之外,还必须有其它几个依赖包:commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph.jar,morph-1.0.1.jar 下面是例子代码: // JSON转换 JSONObject jsonObj = JSONObject.fromObject(jsonStr

  • Java如何将String转换成json对象或json数组

    目录 将String转换成json对象或json数组 字符串转json数组的解决 首先导入 net.sf.json.JSONArray和net.sf.json.JSONObject 两个jar 包 将String转换成json对象或json数组 这里的SmartProejctEquipmentMap 是我自定的一个实体类,可以自己定义转换. 注意:json字符串中键的名称要和实体类一致. @Test public void TestJsonObject() { String datajson =

  • Java获取json数组对象的实例讲解

    如下所示: JSONArray jsonArray1 = jsonObject.getJSONArray("result"); for (int i = 0; i < jsonArray1.length(); i++) { JSONObject temp = (JSONObject) jsonArray1.get(i); String x = temp.getString("x"); String y = temp.getString("y"

  • 详解Java中String JSONObject JSONArray List<实体类>转换

    JSON使用阿里的fastJson为依赖包 gradle依赖管理如下: compile group: 'com.alibaba', name: 'fastjson', version:'1.2.41' 1.String转JSONObject 前言:String 是JSONObject格式的字符串 eg: JSONObject jSONObject = JSONObject.parseObject(String); 2.String转JSONArray 前言:String 是JSONArray格式

  • 使用Java进行Json数据的解析(对象数组的相互嵌套)

    这段时间我们在做一个英语翻译软件的小小小APP,涉及到了对Json数据的解析,所以特地来总结一下! 假设我们要对如下数据进行解析,其实在平时,返回的Json数据是很乱的,很难分清数据的关系,这是经过相关工具美化后的结果 { "translation": [ "爱" ], "basic": { "us-phonetic": "lʌv", "phonetic": "lʌv"

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

    目录 Java 从json提取数组并转换为list Java单个对象和List对象转换成Json,Json转List (一)使用单个对象转换JSON对象 (二)多个对象存到List,再转换成JSON (三)json的list对象转List对象 Java 从json提取数组并转换为list 这里ret表示json字符串原文 // 解析为JSONObject JSONObject jsonObject = JSONObject.parseObject(ret); // 提取出JSONArray JS

  • Java任意长度byte数组转换为int数组的方法

    前言 嗯.最近工程上遇到一个byte数组转换为int的问题,解决过程中遇到了几个坑,经过各种查资料终于还是解决了.撒花. Java的位运算以及byte数组与其他类型数据的转换比c/c++感觉麻烦一些.这里简单说明一下byte数组和int的转换吧. 总所周知,int类型数据在Java中占据32 bit,byte占据8 bit,这样的话,正常情况下4个byte可以转换成一个int类型数据. 当然,不正常情况下,3个byte或者2个byte甚至1个byte都可以转换为int数据,只需要高位补上就行.

  • java实现二维数组转json的方法示例

    本文实例讲述了java实现二维数组转json的方法.分享给大家供大家参考,具体如下: package Tsets; public class erweiTojson { public static void main(String[] args) { String[][] blogList = { {"2008/07/07", "NetBeans New and Cool", "Tim Boudreau"}, {"2008/07/07&

  • java正则表达式实现提取需要的字符并放入数组【ArrayList数组去重复功能】

    本文实例讲述了java正则表达式实现提取需要的字符并放入数组.分享给大家供大家参考,具体如下: 这里演示Java正则表达式提取需要的字符并放入数组,即ArrayList数组去重复功能. 具体代码如下: package com.test.tool; import java.util.ArrayList; import java.util.HashSet; import java.util.regex.*; public class MatchTest { public static void ma

  • Java将json对象转换为map键值对案例详解

    本文的目的是把json串转成map键值对存储,而且只存储叶节点的数据 比如json数据如下: {responseHeader:{status:0,QTime:0},spellcheck:{suggestions:{中国:{numFound:9,startOffset:0,endOffset:2,suggestion:[中国工商银行, 中国人民, 中国国际, 中国农业, 中国市场, 中国经济, 中国人, 中国广播, 中国文化]}},collations:{collation:中国工商银行}}} 如

  • 浅谈在Java中JSON的多种使用方式

    1. 常用的JSON转换 JSONObject 转 JSON 字符串 JSONObject json = new JSONObject(); jsonObject.put("name", "test"); String str = JSONObject.toJSONString(json); JSON字符串转JSON String str = "{\"name\":\"test\"}"; JSONObjec

  • JAVA对象JSON数据互相转换的四种常见情况

    1. 把java 对象列表转换为json对象数组,并转为字符串 复制代码 代码如下: JSONArray array = JSONArray.fromObject(userlist);     String jsonstr = array.toString(); 2.把java对象转换成json对象,并转化为字符串 复制代码 代码如下: JSONObject object = JSONObject.fromObject(invite);    String str=object.toString

随机推荐