JSON的N叉树数据结构如下:
{
"code": 0,
"data": [
{
"name": "xxx",
"uuid": "1xxx",
"subscribe": false,
"children": [
{
"name": "xxx",
"uuid": "2xxx",
"subscribe": false,
"parentId": "1xxx",
"children": [
{
"name": "xxx",
"uuid": "3xxx",
"subscribe": false,
"parentId": "2xxx",
"children": []
},
{
"name": "xxx",
"uuid": "3xxx",
"subscribe": false,
"parentId": "2xxx",
"children": []
}
......
],
"message": "success"
}
可以看到data下面数组数据有很多的相同数据:
{
"name": "xxx",
"uuid": "xxx",
"subscribe": false,
"parentId": "xxx",
"children": []
}
因此可以新建一个实体类来封装这些json数据:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class xxx {
private String name;
private String uuid;
private String subscribe;
private List<SZPlusColumn> children;
private String parentId;
}
接下来把json对象转换成JSONObject类型:
// 请求地址
String pullColumnUrl = "xxx";
// token 直接赋值为 null
String token = null;
// 发起 get 请求同时转换为 JSONObject 类型
JSONObject result = JSONObject.parseObject(HttpUtils.sendGet(pullColumnUrl, token));
获取result里面的data信息:
// 获取 result 里面的 data json 数组
JSONArray jsonArray = result.getJSONArray("data");
将data数组转换为JSONString
// 将 data 数组转换为 JSONString
String data = JSON.toJSONString(jsonArray);
最后再把其转换为 xxx 的 List 集合
// 保存为 xxx 实体类的 List 集合
List<xxx> xxxs = JSON.parseArray(data, xxx.class);
这样就可以得到JSON的N叉树数据结构转换成Java的List数据结构了。
接下来是递归遍历整个List数据结构代码:
public List<xxx> DFSxxx(List<SZPlusColumn> 1xxx, List<xxx> 2xxx) {
int 1xxxLen = 1xxx.size();
for(int i = 0; i < 1xxxLen; i++) {
2xxx.add(new xxx(1xxx.get(i).getUuid(), 1xxx.get(i).getName()));
if(!1xxx.get(i).getChildren().isEmpty()) {
DFSxxx(1xxx.get(i).getChildren(), 2xxx);
}
}
return 2xxx;
}
这样就可以遍历List整个N叉树里面的数据了。