我有一个像这样的 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/