JSON字符串转对象,List集合,需要的jar 包
Maven项目下需要6个jar包,但是经过测试发现有个包无法下载到本地,故从本地导入
1、先写maven 配置jar,此处引入5个jar
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
2、本地导入最重要的jar:
pom.xml 中引入:
<dependency> <groupId>dingding</groupId> <artifactId>dingding</artifactId> <version>2.8</version> <scope>system</scope> <systemPath>${project.basedir}/lib/json-lib-2.1-jdk15.jar</systemPath> </dependency>
groupId,artifactId,version 自定义的 ,scope必须是system
3、打包jar时必须加入以下配置,否则jar不能一起打包
<build> <resources> <resource> <directory>lib</directory> <targetPath>/BOOT-INF/lib/</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </resources> </build>
测试转成对象:
String arrayStr = "[{\"user\":{\"userId\":null,\"uname\":\"zhangsan\",\"pwd\":null,\"email\":null,\"schoolName\":null,\"teamMember\":null,\"roles\":null,\"enable\":null},\"apply\":{\"applyId\":3,\"applyTime\":\"2020-03-23T15:42:36.000+0000\",\"user\":null},\"report\":{\"reportId\":1,\"filesName\":\"1:温湿度、光照数据实时显示(10分)#The0questionFA1866E0CB314B1B912437EDC55948BF_one.apk; 2:建立数据库(15分)#The1question95E407157C0A4BDEBE21F6CE4E0910F5_two.apk;\",\"score\":null,\"uploadTime\":\"2020-03-20T10:27:10.000+0000\",\"user\":null}},{\"user\":{\"userId\":null,\"uname\":\"wangwu\",\"pwd\":null,\"email\":null,\"schoolName\":null,\"teamMember\":null,\"roles\":null,\"enable\":null},\"apply\":{\"applyId\":4,\"applyTime\":\"2020-03-23T15:46:33.000+0000\",\"user\":null},\"report\":null},{\"user\":{\"userId\":null,\"uname\":\"lqy\",\"pwd\":null,\"email\":null,\"schoolName\":\"理工大学\",\"teamMember\":\"王雅雯;\",\"roles\":null,\"enable\":null},\"apply\":{\"applyId\":5,\"applyTime\":\"2020-03-23T16:36:32.000+0000\",\"user\":null},\"report\":null}]"; //转化为list List<UserReportApply> list2= JSONArray.toList(JSONArray.fromObject(arrayStr), UserReportApply.class); for (UserReportApply stu : list2) { System.out.println(stu); }
输出结果:
bean.UserReportApply{user=bean.User{userId=null, uname='zhangsan', pwd='null', email='null', schoolName='null', teamMember='null', roles=null, enable=null}, apply=bean.Apply{applyId=3, applyTime=2020-03-23T15:42:36.000+0000, user=null}, report=bean.Report{reportId=1, filesName='1:温湿度、光照数据实时显示(10分)#The0questionFA1866E0CB314B1B912437EDC55948BF_one.apk; 2:建立数据库(15分)#The1question95E407157C0A4BDEBE21F6CE4E0910F5_two.apk;', score=null, uploadTime=2020-03-20T10:27:10.000+0000, user=null}} bean.UserReportApply{user=bean.User{userId=null, uname='wangwu', pwd='null', email='null', schoolName='null', teamMember='null', roles=null, enable=null}, apply=bean.Apply{applyId=4, applyTime=2020-03-23T15:46:33.000+0000, user=null}, report=null} bean.UserReportApply{user=bean.User{userId=null, uname='lqy', pwd='null', email='null', schoolName='理工大学', teamMember='王雅雯;', roles=null, enable=null}, apply=bean.Apply{applyId=5, applyTime=2020-03-23T16:36:32.000+0000, user=null}, report=null}
结果正确!