复杂嵌套 JSON转 markdown 列表

package com.lyr.easymybatis.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Author lyr
 * @create 2020/12/16 23:13
 */
public class MarkdownUtil {
    private static void json2MarkdownList(StringBuilder output,Object obj,int stack,String key) {
        if (obj instanceof Collection) {
            //数组类型
            output.append(pre(stack));
            //嵌套json类型
            output.append(formatName(key,"list",stack));
            output.append(endl());
            // for(Object nested: (Collection)obj) {
                if (((Collection) obj).size()>0) {
                    json2MarkdownList(output, ((List)obj).get(0),stack+1,"[+]");
                }
            // }
        }else if (obj instanceof Map) {
            output.append(pre(stack));
            //嵌套json类型
            output.append(formatName(key,"Object",stack));
            output.append(endl());
            ((Map) obj).forEach((k,v) -> {
                json2MarkdownList(output, v, stack+1,k.toString());
            });
        }else{
            //单值类型
            output.append(pre(stack));
            String type = "number";
            if (obj instanceof String) type = "String";
            output.append(formatName(key,type,stack));

            output.append(endl());
        }

    }

    private static String formatName(String key,String type,int stack) {
        return key+"   【 type= "+type+" ,required = true , value = "+key +" 】";
    }
    private static String endl() {
        return "\r\n";
    }
    private static String pre(int stack) {
        if (stack<=0) return "- ";
        int len = (stack)*4;
        StringBuilder s = new StringBuilder();
        for (int i=0;i<len;++i) s.append(" ");
        s.append("- ");
        return s.toString();
    }

    public static String json2MarkdownList(String json) {
        JSONObject jsn = JSON.parseObject(json);
        StringBuilder sb = new StringBuilder();
        json2MarkdownList(sb,jsn,-1,"");
        return sb.toString();
    }
}



@Test
    void outPUTMarkdown() {
        String json =  "{\n" +
                "    \"data\": {\n" +
                "        \"total\": 15,\n" +
                "        \"pages\": 3,\n" +
                "        \"recommend\": [\n" +
                "            {\n" +
                "                \"blog\": {\n" +
                "                    \"commentCount\": null,\n" +
                "                    \"id\": 29,\n" +
                "                    \"title\": \"二叉树最大路径和\",\n" +
                "                    \"firstPicture\": null,\n" +
                "                    \"flag\": null,\n" +
                "                    \"views\": null,\n" +
                "                    \"appreciation\": null,\n" +
                "                    \"shareStatement\": null,\n" +
                "                    \"commentabled\": null,\n" +
                "                    \"published\": null,\n" +
                "                    \"recommend\": null,\n" +
                "                    \"createTime\": null,\n" +
                "                    \"updateTime\": null,\n" +
                "                    \"typeId\": null,\n" +
                "                    \"userId\": null,\n" +
                "                    \"tagIds\": null,\n" +
                "                    \"content\": null,\n" +
                "                    \"description\": \"题目描述\\r\\n给定一个二叉树,请计算节点值之和最大的路径的节点值之和是多少。\\r\\n这个路径的开始节点和结束节点可以是二叉树中的任意节点\\r\\n例如:\\r\\n给出以下的二叉树,\\r\\n \\r\\n返回的结果为6\\r\\n示例1\"\n" +
                "                },\n" +
                "                \"type\": null,\n" +
                "                \"tags\": [],\n" +
                "                \"user\": null,\n" +
                "                \"commentCount\": null,\n" +
                "                \"tagIds\": \"\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"blog\": {\n" +
                "                    \"commentCount\": null,\n" +
                "                    \"id\": 28,\n" +
                "                    \"title\": \"分割链表\",\n" +
                "                    \"firstPicture\": null,\n" +
                "                    \"flag\": null,\n" +
                "                    \"views\": null,\n" +
                "                    \"appreciation\": null,\n" +
                "                    \"shareStatement\": null,\n" +
                "                    \"commentabled\": null,\n" +
                "                    \"published\": null,\n" +
                "                    \"recommend\": null,\n" +
                "                    \"createTime\": null,\n" +
                "                    \"updateTime\": null,\n" +
                "                    \"typeId\": null,\n" +
                "                    \"userId\": null,\n" +
                "                    \"tagIds\": null,\n" +
                "                    \"content\": null,\n" +
                "                    \"description\": \"编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前\\r\\n\\r\\n给定一个链表的头指针 ListNode* pHead,请返回重新排列后的链表的头指针。注意:分割以后保持原来的数据顺序不变。\"\n" +
                "                },\n" +
                "                \"type\": null,\n" +
                "                \"tags\": [],\n" +
                "                \"user\": null,\n" +
                "                \"commentCount\": null,\n" +
                "                \"tagIds\": \"\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"blog\": {\n" +
                "                    \"commentCount\": null,\n" +
                "                    \"id\": 27,\n" +
                "                    \"title\": \"dp之 找零\",\n" +
                "                    \"firstPicture\": null,\n" +
                "                    \"flag\": null,\n" +
                "                    \"views\": null,\n" +
                "                    \"appreciation\": null,\n" +
                "                    \"shareStatement\": null,\n" +
                "                    \"commentabled\": null,\n" +
                "                    \"published\": null,\n" +
                "                    \"recommend\": null,\n" +
                "                    \"createTime\": null,\n" +
                "                    \"updateTime\": null,\n" +
                "                    \"typeId\": null,\n" +
                "                    \"userId\": null,\n" +
                "                    \"tagIds\": null,\n" +
                "                    \"content\": null,\n" +
                "                    \"description\": \"题目描述\\r\\nZ国的货币系统包含面值1元、4元、16元、64元共计4种硬币,以及面值1024元的纸币。现在小Y使用1024元的纸币购买了一件价值为N (0 < N \\\\le 1024)N(0<N≤1024)的商品,请问最少他会收到多少硬币?\"\n" +
                "                },\n" +
                "                \"type\": null,\n" +
                "                \"tags\": [],\n" +
                "                \"user\": null,\n" +
                "                \"commentCount\": null,\n" +
                "                \"tagIds\": \"\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"blog\": {\n" +
                "                    \"commentCount\": null,\n" +
                "                    \"id\": 26,\n" +
                "                    \"title\": \"之字形层序遍历\",\n" +
                "                    \"firstPicture\": null,\n" +
                "                    \"flag\": null,\n" +
                "                    \"views\": null,\n" +
                "                    \"appreciation\": null,\n" +
                "                    \"shareStatement\": null,\n" +
                "                    \"commentabled\": null,\n" +
                "                    \"published\": null,\n" +
                "                    \"recommend\": null,\n" +
                "                    \"createTime\": null,\n" +
                "                    \"updateTime\": null,\n" +
                "                    \"typeId\": null,\n" +
                "                    \"userId\": null,\n" +
                "                    \"tagIds\": null,\n" +
                "                    \"content\": null,\n" +
                "                    \"description\": \"给定一个二叉树,返回该二叉树的之字形层序遍历,(第一层从左向右,下一层从右向左,一直这样交替)\\r\\n例如:\\r\\n给定的二叉树是{3,9,20,#,#,15,7},\"\n" +
                "                },\n" +
                "                \"type\": null,\n" +
                "                \"tags\": [],\n" +
                "                \"user\": null,\n" +
                "                \"commentCount\": null,\n" +
                "                \"tagIds\": \"\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"blog\": {\n" +
                "                    \"commentCount\": null,\n" +
                "                    \"id\": 25,\n" +
                "                    \"title\": \"合并K个已排序的链表\",\n" +
                "                    \"firstPicture\": null,\n" +
                "                    \"flag\": null,\n" +
                "                    \"views\": null,\n" +
                "                    \"appreciation\": null,\n" +
                "                    \"shareStatement\": null,\n" +
                "                    \"commentabled\": null,\n" +
                "                    \"published\": null,\n" +
                "                    \"recommend\": null,\n" +
                "                    \"createTime\": null,\n" +
                "                    \"updateTime\": null,\n" +
                "                    \"typeId\": null,\n" +
                "                    \"userId\": null,\n" +
                "                    \"tagIds\": null,\n" +
                "                    \"content\": null,\n" +
                "                    \"description\": \"题目描述\\r\\n合并\\\\ k k 个已排序的链表并将其作为一个已排序的链表返回。分析并描述其复杂度。\"\n" +
                "                },\n" +
                "                \"type\": null,\n" +
                "                \"tags\": [],\n" +
                "                \"user\": null,\n" +
                "                \"commentCount\": null,\n" +
                "                \"tagIds\": \"\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"blog\": {\n" +
                "                    \"commentCount\": null,\n" +
                "                    \"id\": 24,\n" +
                "                    \"title\": \"返回一颗二叉搜索树的第k小节点\",\n" +
                "                    \"firstPicture\": null,\n" +
                "                    \"flag\": null,\n" +
                "                    \"views\": null,\n" +
                "                    \"appreciation\": null,\n" +
                "                    \"shareStatement\": null,\n" +
                "                    \"commentabled\": null,\n" +
                "                    \"published\": null,\n" +
                "                    \"recommend\": null,\n" +
                "                    \"createTime\": null,\n" +
                "                    \"updateTime\": null,\n" +
                "                    \"typeId\": null,\n" +
                "                    \"userId\": null,\n" +
                "                    \"tagIds\": null,\n" +
                "                    \"content\": null,\n" +
                "                    \"description\": \"给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8)    中,按结点数值大小顺序第三小结点的值为4。\"\n" +
                "                },\n" +
                "                \"type\": null,\n" +
                "                \"tags\": [],\n" +
                "                \"user\": null,\n" +
                "                \"commentCount\": null,\n" +
                "                \"tagIds\": \"\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"blog\": {\n" +
                "                    \"commentCount\": null,\n" +
                "                    \"id\": 23,\n" +
                "                    \"title\": \"#栈#括号匹配问题\",\n" +
                "                    \"firstPicture\": null,\n" +
                "                    \"flag\": null,\n" +
                "                    \"views\": null,\n" +
                "                    \"appreciation\": null,\n" +
                "                    \"shareStatement\": null,\n" +
                "                    \"commentabled\": null,\n" +
                "                    \"published\": null,\n" +
                "                    \"recommend\": null,\n" +
                "                    \"createTime\": null,\n" +
                "                    \"updateTime\": null,\n" +
                "                    \"typeId\": null,\n" +
                "                    \"userId\": null,\n" +
                "                    \"tagIds\": null,\n" +
                "                    \"content\": null,\n" +
                "                    \"description\": \"判断括号序列是否合法\"\n" +
                "                },\n" +
                "                \"type\": null,\n" +
                "                \"tags\": [],\n" +
                "                \"user\": null,\n" +
                "                \"commentCount\": null,\n" +
                "                \"tagIds\": \"\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"blog\": {\n" +
                "                    \"commentCount\": null,\n" +
                "                    \"id\": 22,\n" +
                "                    \"title\": \"ps将图片设计稿调节为 横向750px\",\n" +
                "                    \"firstPicture\": null,\n" +
                "                    \"flag\": null,\n" +
                "                    \"views\": null,\n" +
                "                    \"appreciation\": null,\n" +
                "                    \"shareStatement\": null,\n" +
                "                    \"commentabled\": null,\n" +
                "                    \"published\": null,\n" +
                "                    \"recommend\": null,\n" +
                "                    \"createTime\": null,\n" +
                "                    \"updateTime\": null,\n" +
                "                    \"typeId\": null,\n" +
                "                    \"userId\": null,\n" +
                "                    \"tagIds\": null,\n" +
                "                    \"content\": null,\n" +
                "                    \"description\": \"ps 调节 设计稿 横向 750px\"\n" +
                "                },\n" +
                "                \"type\": null,\n" +
                "                \"tags\": [],\n" +
                "                \"user\": null,\n" +
                "                \"commentCount\": null,\n" +
                "                \"tagIds\": \"\"\n" +
                "            }\n" +
                "        ],\n" +
                "        \"contentList\": [\n" +
                "            {\n" +
                "                \"commentCount\": 0,\n" +
                "                \"id\": 29,\n" +
                "                \"title\": \"二叉树最大路径和\",\n" +
                "                \"firstPicture\": \"http://img.itshare.club/20200915222524.png\",\n" +
                "                \"flag\": \"原创\",\n" +
                "                \"views\": 4,\n" +
                "                \"appreciation\": 1,\n" +
                "                \"shareStatement\": 1,\n" +
                "                \"commentabled\": 1,\n" +
                "                \"published\": 1,\n" +
                "                \"recommend\": 1,\n" +
                "                \"createTime\": \"2020-09-15T14:25:49.000+00:00\",\n" +
                "                \"updateTime\": \"2020-09-15T14:25:49.000+00:00\",\n" +
                "                \"typeId\": 13,\n" +
                "                \"userId\": null,\n" +
                "                \"tagIds\": \"12\",\n" +
                "                \"content\": null,\n" +
                "                \"description\": \"题目描述\\r\\n给定一个二叉树,请计算节点值之和最大的路径的节点值之和是多少。\\r\\n这个路径的开始节点和结束节点可以是二叉树中的任意节点\\r\\n例如:\\r\\n给出以下的二叉树,\\r\\n \\r\\n返回的结果为6\\r\\n示例1\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"commentCount\": 0,\n" +
                "                \"id\": 22,\n" +
                "                \"title\": \"ps将图片设计稿调节为 横向750px\",\n" +
                "                \"firstPicture\": \"http://img.itshare.club/20200902000615.webp\",\n" +
                "                \"flag\": \"原创\",\n" +
                "                \"views\": 1,\n" +
                "                \"appreciation\": 1,\n" +
                "                \"shareStatement\": 1,\n" +
                "                \"commentabled\": 1,\n" +
                "                \"published\": 1,\n" +
                "                \"recommend\": 1,\n" +
                "                \"createTime\": \"2020-09-01T16:07:01.000+00:00\",\n" +
                "                \"updateTime\": \"2020-09-08T06:06:16.000+00:00\",\n" +
                "                \"typeId\": 15,\n" +
                "                \"userId\": null,\n" +
                "                \"tagIds\": \"13\",\n" +
                "                \"content\": null,\n" +
                "                \"description\": \"ps 调节 设计稿 横向 750px\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"commentCount\": 0,\n" +
                "                \"id\": 25,\n" +
                "                \"title\": \"合并K个已排序的链表\",\n" +
                "                \"firstPicture\": \"http://img.itshare.club/20200903190846.png\",\n" +
                "                \"flag\": \"原创\",\n" +
                "                \"views\": 1,\n" +
                "                \"appreciation\": 1,\n" +
                "                \"shareStatement\": 1,\n" +
                "                \"commentabled\": 1,\n" +
                "                \"published\": 1,\n" +
                "                \"recommend\": 1,\n" +
                "                \"createTime\": \"2020-09-03T11:09:44.000+00:00\",\n" +
                "                \"updateTime\": \"2020-09-08T06:06:03.000+00:00\",\n" +
                "                \"typeId\": 14,\n" +
                "                \"userId\": null,\n" +
                "                \"tagIds\": \"12\",\n" +
                "                \"content\": null,\n" +
                "                \"description\": \"题目描述\\r\\n合并\\\\ k k 个已排序的链表并将其作为一个已排序的链表返回。分析并描述其复杂度。\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"commentCount\": 0,\n" +
                "                \"id\": 23,\n" +
                "                \"title\": \"#栈#括号匹配问题\",\n" +
                "                \"firstPicture\": \"http://img.itshare.club/20200903011814.png\",\n" +
                "                \"flag\": \"原创\",\n" +
                "                \"views\": 4,\n" +
                "                \"appreciation\": 1,\n" +
                "                \"shareStatement\": 1,\n" +
                "                \"commentabled\": 1,\n" +
                "                \"published\": 1,\n" +
                "                \"recommend\": 1,\n" +
                "                \"createTime\": \"2020-09-02T17:18:55.000+00:00\",\n" +
                "                \"updateTime\": \"2020-09-08T06:05:54.000+00:00\",\n" +
                "                \"typeId\": 14,\n" +
                "                \"userId\": null,\n" +
                "                \"tagIds\": \"12\",\n" +
                "                \"content\": null,\n" +
                "                \"description\": \"判断括号序列是否合法\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"commentCount\": 0,\n" +
                "                \"id\": 19,\n" +
                "                \"title\": \"给定链表,返回环的入口节点\",\n" +
                "                \"firstPicture\": \"http://img.itshare.club/20200829013310.webp\",\n" +
                "                \"flag\": \"原创\",\n" +
                "                \"views\": 0,\n" +
                "                \"appreciation\": 1,\n" +
                "                \"shareStatement\": 1,\n" +
                "                \"commentabled\": 1,\n" +
                "                \"published\": 1,\n" +
                "                \"recommend\": 1,\n" +
                "                \"createTime\": \"2020-08-28T17:33:26.000+00:00\",\n" +
                "                \"updateTime\": \"2020-09-08T06:05:42.000+00:00\",\n" +
                "                \"typeId\": 14,\n" +
                "                \"userId\": null,\n" +
                "                \"tagIds\": \"12\",\n" +
                "                \"content\": null,\n" +
                "                \"description\": \"对于一个给定的链表,返回环的入口节点,如果没有环,返回null\"\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    \"code\": 200,\n" +
                "    \"message\": \"ok\"\n" +
                "}  ";
        System.out.println(MarkdownUtil.json2MarkdownList(json));
    }

效果预览:

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值