gpt4 book ai didi

javascript - 将常规 json 转换为 d3.js 的 flare.json?

转载 作者:行者123 更新时间:2023-11-29 16:15:42 25 4
gpt4 key购买 nike

我有一个像这样的 json:

{
"a": {
"x": {
"y": {
"a": {},
"z": {},
"b": {}
}
},
"c": {},
"b": {
"c": {
"d": {}
}
},
"d": {},
...
}
}

有没有快速转换成flare.json格式的方法?

像这样:

{
"name":"a",
"children":[
{
"name":"x",
"children":
{
"name":"y",
"children":[{"name":"a", "size":0},{"name":"z","size":0},{"name":"b","size":0}]

...
}

谢谢。

最佳答案

为此我想出了一系列正则表达式转换。

 #replace-this      #with-this

^\s*" \{"name":"
: \{\}, \},
: \{\} \}
": \{$ ","children":\[
^\s*\}, \]\},
^\s*\} \]\}


#in that order

然后只删除第一行和最后一行(应该是额外的 {]} )

在应用这些正则表达式转换时,

这个:

{
"a": {
"x": {
"y": {
"a": {},
"z": {},
"b": {}
}
},
"c": {},
"b": {
"c": {
"d": {}
}
},
"d": {}
}

会变成这样:

{
"name": "a",
"children": [{
"name": "x",
"children": [{
"name": "y",
"children": [{
"name": "a"
},
{
"name": "z"
},
{
"name": "b"
}]
}]
},
{
"name": "c"
},
{
"name": "b",
"children": [{
"name": "c",
"children": [{
"name": "d"
}]
}]
},
{
"name": "d"
}]
}

然后可以与一些 d3js 示例一起使用。

关于javascript - 将常规 json 转换为 d3.js 的 flare.json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16463171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com