好久没写博客 了,虽然写的一塌糊涂吧,但也得坚持。今天遇到json 转list集合的问题,随便就记一下
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 对象中的属性一一对应
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/232879.html原文链接:https://javaforall.net