java 对象 (树) 的转换 (目录) JSON 节点。

这篇博客介绍如何在Java中创建一个树形对象,并将其转换为JSON节点。通过面向对象编程,博主详细展示了对象组装成树的过程及遍历树的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用java 创建一个树对象,和遍历树的方法。

这个类是使用面向对象的方式,使用对象组装了一个树。然后使用遍历出来,详情看代码。

package cn.com.chx.website.util;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * Title:  存储栏目对象
 * </p>
 * <p>
 * Description: 栏目对象实体
 * </p>
 * <p>
 * Copyright: Copyright 2015 普巴软件有限公司. All rights reserved.
 * </p>
 * <p>
 * Company: 普巴软件有限公司
 * </p>
 * 
 * @author WangXuDong
 * @date 2015年10月13日
 */
public class CatalogTree { 	
	public static void main(String[] args) {

		//			String string = shuju[i];
		//			System.out.println(string);

		//获取所有的一级菜单
		//String[] shuju1 = new String[]{"004","005","003"};

		CatalogTreeEntity cte = new CatalogTreeEntity(); //保存生成的树对象

		CatalogTreeEntity ce004 = new CatalogTreeEntity();  //服装对象
		ce004.setId("1");
		ce004.setCaId("服装");


		cte = getCataTree(ce004,1);   //查找004开头对象的子栏目树
		ideaCataTree(cte);   //遍历栏目树

	}
	/**
	 *  获取栏目树对象
	 * @param ce 栏目树的根节点栏目对象(没有子栏目)
	 * @param level 栏目从根栏目第level开始建树
	 * @return 返回栏目树对象(含子栏目)
	 */
	public static CatalogTreeEntity getCataTree(CatalogTreeEntity ce,int level){
		level++;
		List<CatalogTreeEntity> ctes = getSubCata(ce.getCaId(),level);
		if(ctes.size()>0){    //ce还有子栏目
			for (int i = 0; i < ctes.size(); i++) {
				ce.setSubctes(ctes);
				getCataTree(ctes.get(i),level);	
			}
			return ce;

		}else{
			return null;
		}
	}
	/**
	 * 获取某个栏目下的子栏目
	 * @param caid 栏目号
	 * @param level 等级
	 * @return 某个栏目的某个等级下的子栏目
	 */
	public static List<CatalogTreeEntity> getSubCata(String caid,int level){
		//在数据中查找以004开头,gradeno 为count的栏目对象即可 以集合或数组方式返回
		List<CatalogTreeEntity> subcec = new ArrayList<CatalogTreeEntity>();
		List<CatalogTreeEntity> ctes = getCataDate();
		for (int i = 0; i < ctes.size(); i++) {
			String s = ctes.get(i).getCaId();
			String[] ss = s.split("-");
			String scaid = ss[0];
			if(ss.length==level){
				for (int j = 1; j < level-1; j++) {
					scaid = scaid + "-" + ss[j];
				}
				if(scaid.equals(caid)){
					CatalogTreeEntity cata = new CatalogTreeEntity();
					cata.setCaId(s);  //获取到的数据放进去
					cata.setLevel(level);
					subcec.add(cata);
				}	
			}

		}
		return  subcec;
	}
	/**
	 * 从数据库获取所有的号赋给栏目对象,以栏目对象的List方式返回
	 * @return 所有的栏目(只含有栏目号的栏目对象)
	 */
	public static List<CatalogTreeEntity> getCataDate(){
		String[] shuju = {"服装","服装-上衣","服装-上衣-大人","服装-下衣"
				,"服装-上衣-小孩","服装-帽子","服装-下衣-鞋子","服装-下衣-鞋子-运动鞋"
				,"服装-下衣-鞋子-球鞋","004-002-002","004-003-001","004-002-001-001"
				,"005","005-001","005-002","005-002-003","003"
				,"003-001","003-002"};
		List<CatalogTreeEntity> cec = new ArrayList<CatalogTreeEntity>();
		for (int i = 0; i < shuju.length; i++) {
			CatalogTreeEntity cte = new CatalogTreeEntity();
			cte.setCaId(shuju[i]);
			cec.add(cte);
		}
		return cec;
	}
	/**
	 * 遍历栏目树
	 */
	public static void ideaCataTree(CatalogTreeEntity cte){
		
		if(cte.getSubctes()!=null){
			for (int i = 0; i < cte.getSubctes().size(); i++) {
				
				CatalogTreeEntity recte =new  CatalogTreeEntity();
				recte = cte.getSubctes().get(i);
				if(recte!=null){
					String tabStr = "";
					int c = recte.getLevel()-1;
					for (int j = 0; j < c; j++) {
						tabStr +="---";
					}
					System.out.println(tabStr+recte.getCaId());
					ideaCataTree(recte);
				}
			}
			
		}
	}

}
这是一个树对象,这个对象中的一个集合字段调用了自身。
package cn.com.chx.website.util;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * Title: 
 * </p>
 * <p>
 * Description: 
 * </p>
 * <p>
 * Copyright: Copyright 2015 普巴软件有限公司. All rights reserved.
 * </p>
 * <p>
 * Company: 普巴软件有限公司
 * </p>
 * 
 * @author WangXuDong
 * @date 2015年10月13日
 */
public class CatalogTreeEntity implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1075366343031430593L;
	private String id;
	private String caId;
	private String caName;
	private int level;
	private List<CatalogTreeEntity> subctes;
	
	
	public List<CatalogTreeEntity> getSubctes() {
		return subctes;
	}
	public void setSubctes(List<CatalogTreeEntity> subctes) {
		this.subctes = subctes;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getCaId() {
		return caId;
	}
	public void setCaId(String caId) {
		this.caId = caId;
	}
	public String getCaName() {
		return caName;
	}
	public void setCaName(String caName) {
		this.caName = caName;
	}
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值