解析递归JSON,来自Markdown Ast
递归功能不是我的堡垒,我很确定这里需要什么。
我有一个嵌套的JSON对象,该对象表示嵌套的清单 It is autogenerated from markdown using mdast-util-gfm-task-列表项目
* AAA
* BBB
* CCC
* [x] DDD
* [ ] EEE
* FFF
{
"type": "root",
"children": [
{
"type": "list",
"ordered": false,
"start": null,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "AAA"
}
]
},
{
"type": "list",
"ordered": false,
"start": null,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "BBB"
}
]
}
]
}
]
}
]
},
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "CCC"
}
]
},
{
"type": "list",
"ordered": false,
"start": null,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "DDD"
}
]
},
{
"type": "list",
"ordered": false,
"start": null,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "EEE"
}
]
}
]
},
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "FFF"
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
我需要操纵它以采用以下内容,更多的共识格式:
{
"type" : "list",
"data" : {
"style" : "unordered",
"items" : [
{
"content": "AAA",
"checked" : null
"items": [
{
"content": "BBB",
"checked" : null
"items": []
}
]
},
{
"content": "CCC",
"checked" : null
"items": [
{
"content": "DDD",
"checked": true,
"items": [
{
"content": "EEE",
"checked": false,
"items": []
},
{
"content": "FFF",
"checked": null,
"items": []
}
]
},
]
}
]
}
}
任何帮助都非常感谢 - 我一直在墙上撞了几个小时。
Recursive functions are not my forté, and im pretty sure thats what is needed here.
I have a nested json object, which represents a nested checklist
It is autogenerated from markdown using mdast-util-gfm-task-list-item
* AAA
* BBB
* CCC
* [x] DDD
* [ ] EEE
* FFF
{
"type": "root",
"children": [
{
"type": "list",
"ordered": false,
"start": null,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "AAA"
}
]
},
{
"type": "list",
"ordered": false,
"start": null,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "BBB"
}
]
}
]
}
]
}
]
},
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "CCC"
}
]
},
{
"type": "list",
"ordered": false,
"start": null,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "DDD"
}
]
},
{
"type": "list",
"ordered": false,
"start": null,
"spread": false,
"children": [
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "EEE"
}
]
}
]
},
{
"type": "listItem",
"spread": false,
"checked": null,
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "FFF"
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
I need to manipulate it to be in the following, more consise format:
{
"type" : "list",
"data" : {
"style" : "unordered",
"items" : [
{
"content": "AAA",
"checked" : null
"items": [
{
"content": "BBB",
"checked" : null
"items": []
}
]
},
{
"content": "CCC",
"checked" : null
"items": [
{
"content": "DDD",
"checked": true,
"items": [
{
"content": "EEE",
"checked": false,
"items": []
},
{
"content": "FFF",
"checked": null,
"items": []
}
]
},
]
}
]
}
}
Any help greatly appreciated - I have been banging my head against the wall for a couple of hours now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想我在这里很近,但无法完成:
I think I am pretty close here but haven't been able to finish it off:
谢谢@jimthedev,
我还在另一个论坛上获得了一些帮助,这两个答案都很有用。
最后,我很成功地产生了以下功能,该功能几乎非常适合我的用例。
Thanks @jimTheDev
I also recived some assistance on another forum, and both answers were useful.
In the end, I have sucssfully produced the following function, which works almost perfectly for my use case.
我一直使用的版本类似于您自己想出的版本,但是它以不同的编码方式完成,并且从
/root/data/data/toce/code>中删除了一层数组,该数组
t在您要求的输出中,没有太多意义。我还将输入更改为表示
检查:true
forddd
和检查:
eee> eee
以匹配您的示例标记。这两个初始AST具有null
。我假设null
是针对“此处无复选框”,并且true
和false
如果检查或未检查,则将使用。如果不是这种情况,那可能需要一些摆弄。The version I had been working on is similar to what you figured out yourself, but it's done in a different coding style, and it removes one layer of array from
/root/data/items
, which wasn't there in your requested output and doesn't make much sense.I also changed the input to represent
checked: true
forDDD
andchecked: false
forEEE
to match your sample markup. The initial AST hadnull
for both of those. I'm assuming thatnull
is meant for "no checkbox here" andtrue
andfalse
would be used if there is one checked or unchecked. If that's not the case, then it might take a bit of fiddling.