json 转list 集合

json 转list 集合好久没写博客了 虽然写的一塌糊涂吧 但也得坚持 今天遇到 json 转 list 集合的问题 随便就记一下 importnet sf json JSONArray importnet sf json JSONObject pom xml 文件 lt json 依赖 gt lt dependency gt amp l

好久没写博客 了,虽然写的一塌糊涂吧,但也得坚持。今天遇到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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号