如何在JSON中将此数据编码为父/子结构 [英] how to encode this data to parent / children structure in JSON

查看:130
本文介绍了如何在JSON中将此数据编码为父/子结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用d3.js以动画(生物体)(生物体)家族(一次最多4000个)作为树形图,虽然数据源也可以是目录列表或命名空间对象列表。我的资料如下:

  json = {
organism:[
{name:'Hemiptera.Miridae 。},
{name:'Hemiptera.Miridae.Neophloeobia.incisa'},
{name:'Lepidoptera.Nymphalidae.Ephinephile.rawnsleyi'},
...等。 ..
]
}

我的问题是:将上述数据转换为层次父/子数据结构的最佳方式,由多个d3可视化对象使用,例如 treemap (有关数据示例,请参见 flare .json 在d3 / examples / data /目录中)。
以下是所需数​​据结构的示例:

  {name:ROOT,
children:[
{name:Hemiptera,
children:[
{name:Miridae,
children
{name:Kanakamiris,children:[]},
{name:Neophloeobia,
children:[
{name :incisa,children:[]}
]}
]}
]},
{name:Lepidoptera,
children:[
{name:Nymphalidae,
children:[
{name:Ephinephile,
children:[
{name:rawnsleyi,children:[]}
]}
]}
]}
]}
}

编辑:将所有原始的所需数据结构包含在 ROOT 节点,以便符合只有一个主父节点的d3示例的结构。



我想了解一个一般的设计模式,一个奖金我会喜欢看到一些解决方案在javascript,php,(或甚至python)。 javascript是我的首选项。
对于php:我实际使用的数据来自通过php脚本调用数据库,将结果编码为json。
数据库结果的php脚本是一个有序数组(见下文)如果这是基于php的答案的任何用途。

  Array 

[0] => Array

['Rank_Order'] =>'半翅目'
['Rank_Family'] => ;'Miridae'
['Rank_Genus'] =>'Kanakamiris'
['Rank_Species'] =>''
)........

其中:
'Rank_Order' isParentOf 'Rank_Family' isParentOf 'Rank_Genus' isParentOf 'Rank_Species' p>

我问了一个类似的问题,集中在一个php解决方案这里,但唯一的答案是不是在我的服务器上工作,我不太明白发生了什么,所以我想从设计模式的角度来问这个问题,

解决方案

以下是特定于你的结构的引用我的实际使用是在javascript和d3.js。可以相当容易地使其更通用。我相信可以简化 addChild 函数。希望评论是有帮助的。

  function toHeirarchy(obj){

//获取生物
var orgName,orgNames = obj.organisms;

//创建根对象
var root = {name:'ROOT',children:[]};

//对于每个有机体,获得名称部分
for(var i = 0,iLen = orgNames.length; i orgName = orgNames [ i] .name.split('。');

//从root.children开始
children = root.children;

//对于名称的每个部分,如果已经有
,则获取child如果没有
,则添加新对象和子对象(var j = 0,jLen = orgName .length; j children = addChild(children,orgName [j]);
}
}
return root;

//帮助函数,遍历寻找
//的孩子。如果找到,返回其子数组,否则添加一个新的
//子对象和子数组并返回它。
function addChild(children,name){

//在孩子中查找名称
for(var i = 0,iLen = children.length; i
//如果找到名称,返回其子数组
if(children [i] .name == name){
return children [i] .children;
}
}
//如果没有找到名称,添加一个新对象和
//返回其子数组
children.push({'name' :name,'children':[]});
return children [children.length - 1] .children;
}
}


I am working with d3.js to visualise families of animals (organisms) (up to 4000 at a time) as a tree graph, though the data source could just as well be a directory listing, or list of namespaced objects. my data looks like:

json = {
    organisms:[
        {name: 'Hemiptera.Miridae.Kanakamiris'},
        {name: 'Hemiptera.Miridae.Neophloeobia.incisa'},
        {name: 'Lepidoptera.Nymphalidae.Ephinephile.rawnsleyi'},
        ... etc ...
    ]
}

my question is: I am trying to find the best way to convert the above data to the hierarchical parent / children data structure as is used by a number of the d3 visualisations such as treemap (for data example see flare.json in the d3/examples/data/ directory). Here is an example of the desired data structure:

{"name": "ROOT",
 "children": [
        {"name": "Hemiptera",
         "children": [
             {"name": "Miridae",
              "children": [
                  {"name": "Kanakamiris", "children":[]},
                  {"name": "Neophloeobia",
                   "children": [
                       {"name": "incisa", "children":[] }
                   ]}
              ]}
         ]},
        {"name": "Lepidoptera",
         "children": [
             {"name": "Nymphalidae",
              "children": [
                  {"name": "Ephinephile",
                   "children": [
                       {"name": "rawnsleyi", "children":[] }
                   ]}
              ]}
         ]}
    ]}
}

EDIT: enclosed all the original desired data structure inside a ROOT node, so as to conform with the structure of the d3 examples, which have only one master parent node.

I am looking to understand a general design pattern, and as a bonus I would love to see some solutions in either javascript, php, (or even python). javascript is my preference. In regards to php: the data I am actually using comes from a call to a database by a php script that encodes the results as json. database results in the php script is an ordered array (see below) if that is any use for php based answers.

Array
(
    [0] => Array
        (
            ['Rank_Order'] => 'Hemiptera'
            ['Rank_Family'] => 'Miridae'
            ['Rank_Genus'] => 'Kanakamiris'
            ['Rank_Species'] => ''
        ) ........

where: 'Rank_Order' isParentOf 'Rank_Family' isParentOf 'Rank_Genus' isParentOf 'Rank_Species'

I asked a similar question focussed on a php solution here, but the only answer is not working on my server, and I dont quite understand what is going on, so I want to ask this question from a design pattern perspective, and to include reference to my actual use which is in javascript and d3.js.

解决方案

The following is specific to the structure you've provided, it could be made more generic fairly easily. I'm sure the addChild function can be simplified. Hopefully the comments are helpful.

function toHeirarchy(obj) {

  // Get the organisms array
  var orgName, orgNames = obj.organisms;

  // Make root object
  var root = {name:'ROOT', children:[]};

  // For each organism, get the name parts
  for (var i=0, iLen=orgNames.length; i<iLen; i++) {
    orgName = orgNames[i].name.split('.');

    // Start from root.children
    children = root.children;

    // For each part of name, get child if already have it
    // or add new object and child if not
    for (var j=0, jLen=orgName.length; j<jLen; j++) {
      children = addChild(children, orgName[j]);      
    }
  }
  return root;

  // Helper function, iterates over children looking for 
  // name. If found, returns its child array, otherwise adds a new
  // child object and child array and returns it.
  function addChild(children, name) {

    // Look for name in children
    for (var i=0, iLen=children.length; i<iLen; i++) {

      // If find name, return its child array
      if (children[i].name == name) {
        return children[i].children;        
      }
    }
    // If didn't find name, add a new object and 
    // return its child array
    children.push({'name': name, 'children':[]});
    return children[children.length - 1].children;
  }
}

这篇关于如何在JSON中将此数据编码为父/子结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆