![](/img/trans.png)
[英]Converting a regular json file to a parent-child hierarchical json as used by d3 flare.json
[英]Convert regular json to flare.json for d3.js?
我有一个像这样的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示例结合使用。
您将必须编写自己的JSON或动态更改JSON的函数。 Flare.json只是遵循遵循Mike Bostock的d3文件的架构。
我会给你一个提示。 您编写的架构似乎是(以psuedocode表示)
array("name":"a", "children":array("name":"x","children":array(.....
基本上,您需要创建一个多维数组才能获得所需的结果。 不幸的是,我不知道您如何获取数据,因此我无法告诉您更多信息。 如果使用php,请使用json_encode方法
echo json_encode($jsonArray)
或在javascript中使用json.stringify
var json = JSON.stringify($jsonArray)
为了使数组变成json。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.