树中 children 转化为不同的列
原数据结构
原始数据为嵌套的 JSON 数组结构,每个节点包含 id、label 和 children 字段,children 用于存储子节点。
转换后数据结构
转换后的数据为扁平列表,每一层级对应一列(例如 label1、label2),便于表格展示。
代码实现
package com.platform.project.zhjd;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class A {
public static int deep = 1;
public static int num = 0;
public static int count = 0;
public static JSONArray tableData = new JSONArray();
public static JSONObject itemObj = new JSONObject();
public static void test(JSONArray children) {
for (int i ; i < children.size(); i++) {
children.getJSONObject(i);
jsonObject.getJSONArray();
itemObj = JSONObject.parseObject(itemObj.toJSONString());
itemObj.put(,jsonObject.getString());
itemObj.put(+deep,jsonObject.getString());
(children1 != && children1.size() > ){
count = children1.size();
deep ++;
test(children1);
}{
tableData.add(itemObj);
num ++;
;
}
itemObj = ();
deep = ;
}
}
{
,\;
JSONArray.parseArray(str);
jsonArray.getJSONObject().getJSONArray();
test(children);
System.out.println(tableData);
}
}


