当前位置:编程学习 > JAVA >>

如何将一个表生成JSON树型结构?

已经搜索过论坛,没有找到答案,谢谢! --------------------编程问答-------------------- 层级查询就可以了 --------------------编程问答-------------------- 可以参考:http://download.csdn.net/download/luck332/995421 --------------------编程问答-------------------- 我是放到 MAP LIST 里面通过GSON生成 --------------------编程问答-------------------- 哈哈,是从数据库表里读出层级结构然后生成树吧?
这个要自己写算法的,网上当然别想找到。 --------------------编程问答-------------------- Category.java-获取数据源并构造为树的算法:
package com.biristone.service.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.biristone.service.ProTypeService;
import com.biristone.pojo.ProType;

public class Category {
private static String configFile = "applicationContext-services.xml";
private static ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
    private static Map<Long, Category> catMap = new HashMap<Long, Category>();
private static ProTypeService ps = (ProTypeService)context.getBean("proTypeService");
private static List<ProType> ptp;
    
static 
{
BuildAllCategory(-1);
}
    //根据产品分类构造树
    public static void BuildAllCategory(int id)
    {
        ptp = ps.getTypeByPid(id);
        List<Category> childs = new ArrayList<Category>(); 
        //如果没有父节点,则为根节点
    if(id==-1)
    {
    new Category(0,ptp.get(0).getName());
    BuildAllCategory(ptp.get(0).getTid());
    }
    else
    {
    for(ProType node:ptp)
    {
    childs.add(new Category((long)node.getTid(),node.getName()));
    }
    //挂到相应的父节点
    Category.getById((long)id).setChildren(childs);
   
    //如果子节点还有子节点
    for(Category child:childs)
    {
    if(null!=ps.getTypeByPid((int)child.getId()))
    {
    BuildAllCategory((int)child.getId());
    }
    }
    }  
    }
    
    public static Category getById(long id) {
        return catMap.get(id);
    }

    private Long id;
    private String name;
    private List<Category> children;
    private boolean toggle;

    public Category(long id, String name, Category... children) {
        this.id = id;
        this.name = name;
        this.children = new ArrayList<Category>();
        for (Category child : children) {
            this.children.add(child);
        }

        catMap.put(id, this);
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Category> getChildren() {
        return children;
    }

    public void setChildren(List<Category> children) {
        this.children = children;
    }

    public void toggle() {
        toggle = !toggle;
    }

    public boolean isToggle() {
        return toggle;
    }
}

tp_type表:tid为分类,pid为上级分类,name为分类名
Category.getById(根节点编号)

就返回整个分类树了

关键部分都列出来了,里面的表结构映射的bean和action、Category转成json就不用我多说了吧。

--------------------编程问答-------------------- 我记得easyui 里面好像有这样的例子 --------------------编程问答-------------------- mongoDB都是这样。 --------------------编程问答-------------------- 把你的表导入mongoDB吧
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,