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

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

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

这里ret表示json字符串原文

1
2
3
4
5
6
7
// 解析为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 list = JSONObject.parseArray(str, Org.class);

使用getJSONArray()获取到jsonarray后,再将jsonArray转换为字符串,最后将字符串解析为List列表。

代码中的Org是我List列表中元素的类型。

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
2
3
4
5
6
7
8
9
10
11
12
@Test
public void getArrayList(){//测试转换成Json,List转换成JSONList
    List vouchersList=new ArrayList();
    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提取数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Test
public void test1210() {
    List vouchers=new ArrayList();
    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

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

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/code/java/6853.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部