json 转list 集合

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

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

pom.xml 文件:

        
        <dependency>
            <groupId>net.sf.json-libgroupId>
            <artifactId>json-libartifactId>
            <classifier>jdk15classifier>
        dependency>
        <dependency>
            <groupId>net.sf.ezmorphgroupId>
            <artifactId>ezmorphartifactId>
        dependency>
        <dependency>
            <groupId>commons-collectionsgroupId>
            <artifactId>commons-collectionsartifactId>
        dependency>
        <dependency>
            <groupId>commons-beanutilsgroupId>
            <artifactId>commons-beanutilsartifactId>
        dependency>
        


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


java

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

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


List 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 对象中的属性一一对应

你可能感兴趣的:(java)