json 转list 集合

好久没写博客 了,虽然写的一塌糊涂吧,但也得坚持。今天遇到json 转list集合的问题,随便就记一下

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

pom.xml 文件:

        <!-- json依赖 -->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <classifier>jdk15</classifier>
        </dependency>
        <dependency>
            <groupId>net.sf.ezmorph</groupId>
            <artifactId>ezmorph</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
        </dependency>
        <!-- json依赖 end -->


对应版本
        <json-lib.version>2.4</json-lib.version>
        <ezmorph.version>1.0.6</ezmorph.version>
        <commons-collections.version>3.2.1</commons-collections.version>
        <commons-beanutils.version>1.9.3</commons-beanutils.version>


java

customized 值为: [{“cusId”:”999”,”orderNum”:1},{“cusId”:”998”,”orderNum”:2}]

String customized = " [{\"cusId\":\"999\",\"orderNum\":1},{\"cusId\":\"998\",\"orderNum\":2}]"


List<Customized> list1 = new ArrayList();  
// 先把json 字符串转换为json 数组
        JSONArray jsonArray1 = JSONArray.fromObject(customized);  
//循环获取json数组中的 json 对象,然后转换为 object
        for (int i = 0; i < jsonArray1.size(); i++) {  
            JSONObject jsonObject2 = jsonArray1.getJSONObject(i);  
            Customized cust = (Customized) JSONObject.toBean(jsonObject2, Customized.class);  
            list1.add(cust);  
        }  

json 字符串中的属性 必须 和 Customized 对象中的属性一一对应

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值