- 首先通过Maven导入包:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
或者不通过Maven,自己去下载,网址:https://github.com/alibaba/fastjson
- 操作步骤
//初始化List
List<AuthTreeInfoDTO> menuList= new ArrayList<AuthTreeInfoDTO>();
....此处省略add元素操作
//第一步: 将list集合转换为JSON类型的字符串
String s = JSON.toJSONString(menuList); //参数为你的list对象
//第二步:将这个字符串对象s转换为List集合
List<AuthTreeInfoDTO> authTreeInfoDTOList = JSONObject.parseArray(s, AuthTreeInfoDTO.class); //第一个参数为JSON型的字符串,第二个为list集合中的泛型类