java递归生成一颗树的子节点的json数据

	//第一个参数,需要生成树的子节点的JSONArray,第二个参数为树的根节点的JSONObject 
	public static JSONObject getJsonTree( JSONArray json, JSONObject root ){
		JSONArray tempJson = JSONArray.fromObject( "[]" );

		//筛选出 parentId 等于 上一层的 id 的 json 对象
		for( int i = 0; i < json.size(); i++ ){
			if( json.getJSONObject( i ).get( "parentId" ).equals( root.get( "id" ) ) ) {
				tempJson.add( json.getJSONObject( i ) );
			}
		}

		// 减少集合的数量,避免重复查询
		json.removeAll( tempJson );

		for( int i = 0; i < tempJson.size(); i++ ){
			//对第二层进行递归,此处类推
			getJsonTree( json, tempJson.getJSONObject( i ) );
		}

		//生成完的树结构集合加到根节点
		if( tempJson.size() != 0 ){
			root.put( "nodes", tempJson );
		}

		return root;
	}


	// 
	public static void main(String[] a){
		JSONObject root = new JSONObject();
		root.put( "id", "1" );
		root.put( "text", "第1层 1" );

		JSONArray json = new JSONArray();

		JSONObject obj1 = new JSONObject();
		obj1.put( "parentId", "1" );
		obj1.put( "text", "第2层 1" );
		obj1.put( "id", "21" );

		JSONObject obj2 = new JSONObject();
		obj2.put( "parentId", "1" );
		obj2.put( "text", "第2层 2" );
		obj2.put( "id", "22" );

		json.add( obj1 );
		json.add( obj2 );

		JSONObject obj3 = new JSONObject();
		obj3.put( "parentId", "21" );
		obj3.put( "text", "第3层 1" );
		obj3.put( "id", "31" );

		JSONObject obj4 = new JSONObject();
		obj4.put( "parentId", "22" );
		obj4.put( "text", "第3层 2" );
		obj4.put( "id", "32" );

		json.add( obj3 );
		json.add( obj4 );

		JSONObject obj5 = new JSONObject();
		obj5.put( "parentId", "31" );
		obj5.put( "text", "第4层 1" );
		obj5.put( "id", "41" );

		json.add( obj5 );

		root = getJsonTree( json, root );

		System.out.println( root );

	}

得到的结果是:

{
    "id": "1",
    "text": "第1层 1",
    "nodes": [
        {
            "parentId": "1",
            "text": "第2层 1",
            "id": "21",
            "nodes": [
                {
                    "parentId": "21",
                    "text": "第3层 1",
                    "id": "31",
                    "nodes": [
                        {
                            "parentId": "31",
                            "text": "第4层 1",
                            "id": "41"
                        }
                    ]
                }
            ]
        },
        {
            "parentId": "1",
            "text": "第2层 2",
            "id": "22",
            "nodes": [
                {
                    "parentId": "22",
                    "text": "第3层 2",
                    "id": "32"
                }
            ]
        }
    ]
}

转自:  https://blog.csdn.net/qq_34626097/article/details/83756710

--------------------------------------------------------------------------

如果使用List和Map,需要这样写:

    // 第一个参数,需要生成树的节点的List,第二个参数为树的根节点Map
    public static Map<String, Object> getJsonTree2( List<Map<String, Object>> json, Map<String, Object> root ) {
        List<Map<String, Object>> tempJson = new ArrayList<>();

        // 筛选出 parentId 等于 上一层的 id 的 json
        for ( int i = 0; i < json.size(); i++ ) {
            Map<String, Object> map1 = json.get( i );
            
            if ( map1.get( "parentId" ).equals( root.get( "id" ) ) ) {
                tempJson.add( json.get( i ) );
            }
        }

        // 减少集合的数量,避免重复查询
        json.removeAll( tempJson );

        for ( int i = 0; i < tempJson.size(); i++ ) {
            //对第二层进行递归,此处类推
            getJsonTree2( json, tempJson.get( i ) );
        }

        //生成完的树结构集合加到根节点
        if ( tempJson.size() != 0 ) {
            root.put( "nodes", tempJson );
        }

        return root;
    }

 测试,调用这个getJsonTree2方法:

    // main
    public static void main( String[] arr ) {
        Map root = new HashMap();
        root.put( "id", "1" );
        root.put( "text", "第1层 1" );
        
        Map obj1 = new HashMap();
        obj1.put( "parentId", "1" );
        obj1.put( "text", "第2层 1" );
        obj1.put( "id", "21" );
    
        Map obj2 = new HashMap();
        obj2.put( "parentId", "1" );
        obj2.put( "text", "第2层 2" );
        obj2.put( "id", "22" );
    
        List json = new ArrayList();
        
        json.add( obj1 );
        json.add( obj2 );
    
        Map obj3 = new HashMap();
        obj3.put( "parentId", "21" );
        obj3.put( "text", "第3层 1" );
        obj3.put( "id", "31" );
    
        Map obj4 = new HashMap();
        obj4.put( "parentId", "22" );
        obj4.put( "text", "第3层 2" );
        obj4.put( "id", "32" );
    
        json.add( obj3 );
        json.add( obj4 );
    
        Map obj5 = new HashMap();
        obj5.put( "parentId", "31" );
        obj5.put( "text", "第4层 1" );
        obj5.put( "id", "41" );
    
        json.add( obj5 );
    
        root = getJsonTree2( json, root );
    
        System.out.println( root );
    
        System.out.println();
    
        System.out.println( root.get( "nodes" ) );
    }

--------------------------------------------------------------------------

net.sf.json.JSONObject的maven依赖:

        <dependency>
		<groupId>net.sf.json-lib</groupId>
		<artifactId>json-lib</artifactId>
		<version>2.4</version>
		<classifier>jdk15</classifier>
	</dependency>

转换json字符串为net.sf.json.JSONArray:        JSONArray jsonArr = JSONArray.fromObject( jsonStr );
转换json字符串为net.sf.json.JSONObject:      JSONObject jsonObj = JSONObject.fromObject( jsonStr );

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值