1、json数据(目标)
[{ label: '一级 1', children: [{ label: '二级 1-1', children: [{ label: '三级 1-1-1' }] }] }, { label: '一级 2', children: [{ label: '二级 2-1', children: [{ label: '三级 2-1-1' }] }, { label: '二级 2-2', children: [{ label: '三级 2-2-1' }] }] }, { label: '一级 3', children: [{ label: '二级 3-1', children: [{ label: '三级 3-1-1' }] }, { label: '二级 3-2', children: [{ label: '三级 3-2-1' }] }] }]View Code
2、基础类型
package test; import java.util.List; /** * chao */ public class DataItem { private String label; private List<DataItem> children; public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public List<DataItem> getChildren() { return children; } public void setChildren(List<DataItem> children) { this.children = children; } }View Code
package test; /** * 代表数据库里的表 */ public class DataModel { private Integer id; private String label; private Integer pId; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public Integer getPId() { return pId; } public void setPId(Integer pId) { this.pId = pId; } public DataModel(Integer id, String label, Integer pId) { this.id = id; this.label = label; this.pId = pId; } }View Code
3、手动拼接树形结构
// 手动拼接json格式 { List<DataItem> list = new ArrayList<DataItem>(); //第一条数据 { DataItem data = new DataItem(); data.setLabel("一级 1"); List<DataItem> data_child = new ArrayList<DataItem>(); data.setChildren(data_child); DataItem data_child_Data1 = new DataItem(); data_child_Data1.setLabel("二级 1-1"); List<DataItem> data_child_Data1_child = new ArrayList<DataItem>(); data_child_Data1.setChildren(data_child_Data1_child); data_child.add(data_child_Data1); DataItem child1Data1_child_Data1 = new DataItem(); child1Data1_child_Data1.setLabel("三级 1-1-1"); data_child_Data1_child.add(child1Data1_child_Data1); list.add(data); } //第二条数据 { DataItem data = new DataItem(); data.setLabel("一级 2"); List<DataItem> data_child = new ArrayList<DataItem>(); data.setChildren(data_child); //第一条 { DataItem data_child_Data1 = new DataItem(); data_child_Data1.setLabel("二级 2-1"); List<DataItem> data_child_Data1_child = new ArrayList<DataItem>(); data_child_Data1.setChildren(data_child_Data1_child); data_child.add(data_child_Data1); DataItem child1Data1_child_Data1 = new DataItem(); child1Data1_child_Data1.setLabel("三级 2-1-1"); data_child_Data1_child.add(child1Data1_child_Data1); } //第二条 { DataItem data_child_Data1 = new DataItem(); data_child_Data1.setLabel("二级 2-2"); List<DataItem> data_child_Data1_child = new ArrayList<DataItem>(); data_child_Data1.setChildren(data_child_Data1_child); data_child.add(data_child_Data1); DataItem child1Data1_child_Data1 = new DataItem(); child1Data1_child_Data1.setLabel("三级 2-2-1"); data_child_Data1_child.add(child1Data1_child_Data1); } list.add(data); } //第三条数据 { DataItem data = new DataItem(); data.setLabel("一级 3"); List<DataItem> data_child = new ArrayList<DataItem>(); data.setChildren(data_child); //第一条 { DataItem data_child_Data1 = new DataItem(); data_child_Data1.setLabel("二级 3-1"); List<DataItem> data_child_Data1_child = new ArrayList<DataItem>(); data_child_Data1.setChildren(data_child_Data1_child); data_child.add(data_child_Data1); DataItem child1Data1_child_Data1 = new DataItem(); child1Data1_child_Data1.setLabel("三级 3-1-1"); data_child_Data1_child.add(child1Data1_child_Data1); } //第二条 { DataItem data_child_Data1 = new DataItem(); data_child_Data1.setLabel("二级 3-2"); List<DataItem> data_child_Data1_child = new ArrayList<DataItem>(); data_child_Data1.setChildren(data_child_Data1_child); data_child.add(data_child_Data1); DataItem child1Data1_child_Data1 = new DataItem(); child1Data1_child_Data1.setLabel("三级 3-2-1"); data_child_Data1_child.add(child1Data1_child_Data1); } list.add(data); } System.out.println("----------手动拼接----------"); System.out.println(JSON.toJSONString(list));View Code
4、递归拼接
//原始数据 List<DataModel> dataList = new ArrayList(); { dataList.add(new DataModel(1, "一级 1", 0)); dataList.add(new DataModel(2, "二级 1-1", 1)); dataList.add(new DataModel(3, "三级 1-1-1", 2)); dataList.add(new DataModel(4, "一级 2", 0)); dataList.add(new DataModel(5, "二级 2-1", 4)); dataList.add(new DataModel(6, "三级 2-1-1", 5)); dataList.add(new DataModel(7, "二级 2-2", 4)); dataList.add(new DataModel(8, "三级 2-2-1", 7)); dataList.add(new DataModel(9, "一级 3", 0)); dataList.add(new DataModel(10, "二级 3-1", 9)); dataList.add(new DataModel(11, "三级 3-1-1", 10)); dataList.add(new DataModel(12, "二级 3-2", 9)); dataList.add(new DataModel(13, "三级 3-2-1", 12)); } //树结构结果 List<DataItem> treeList = new ArrayList<DataItem>(); treeList = GetTreeData(dataList, 0); System.out.println("----------递归拼接----------"); System.out.println(JSON.toJSONString(treeList));
/** * 递归 * * @param sourceDataList * @param pId * @return */ private static List<DataItem> GetTreeData(List<DataModel> sourceDataList, Integer pId) { List<DataItem> returnList = new ArrayList<DataItem>(); for (Integer i = 0; i < sourceDataList.size(); i++) { DataModel itemData = sourceDataList.get(i); if (itemData.getPId().equals(pId)) { DataItem item = new DataItem(); item.setLabel(itemData.getLabel()); List children = GetTreeData(sourceDataList, itemData.getId()); if (children.size() > 0) item.setChildren(children); returnList.add(item); } } return returnList; }
标签:java,DataItem,Data1,树形,add,拼接,child,new,data From: https://www.cnblogs.com/mulans/p/17417572.html