我必须遍历下面的输入JSON以建立示例子JSON中的父子关系。
逻辑,即Instant_parent_id为-1的元素成为根节点。比根节点Instant_dependents更重要。现在,我必须选择Instant_Depends的每个从属,并寻找匹配的node_id,并且特定元素Instant_Depends成为其子代。这样,我必须遍历到JSON的第n级。实现此目标的最佳方法是什么。我不想使用jQuery。期待使用javascript解决。
输入JSON
[
{
"node_id":1,
"token":"Google",
"posTag":"NNP",
"depLabel":"compound",
"node_role":{
},
"verb_info":[
],
"action_verb_roleset":"",
"parent_node":{
"parent_node":2,
"name":"News"
},
"entity_tag":{
"originalTag":"B-ORG",
"prefix":"B",
"position":"begin",
"mainCategory":"ORG",
"entityDescription":"Organization",
"examples":"Government Agency, Broadcaster, Company, Educational Institution, Employers Organization, Non-Profit Organization, Samba School, Sports League, Sports Team, Website"
},
"siblings":[
],
"dependents":[
],
"pobj_dependents":[
],
"dobj_dependents":[
],
"nobj_dependents":[
],
"parents":[
2,
3
],
"immediate_dependents":[
],
"isRoot":false,
"immediate_parent_id":2,
"parentVerbID":3
},
{
"node_id":2,
"token":"News",
"posTag":"NNP",
"depLabel":"nsubj",
"node_role":{
"parent_verb_id":3,
"grandparent_verb_id":3,
"role":"topic"
},
"verb_info":[
],
"action_verb_roleset":"be.01",
"parent_node":{
"parent_node":3,
"name":"is"
},
"entity_tag":{
"originalTag":"L-ORG",
"prefix":"L",
"position":"last",
"mainCategory":"ORG",
"entityDescription":"Organization",
"examples":"Government Agency, Broadcaster, Company, Educational Institution, Employers Organization, Non-Profit Organization, Samba School, Sports League, Sports Team, Website"
},
"siblings":[
6,
12
],
"dependents":[
{
"dependent":1,
"name":"Google",
"depLabel":"compound"
}
],
"pobj_dependents":[
],
"dobj_dependents":[
],
"nobj_dependents":[
],
"parents":[
3
],
"immediate_dependents":[
{
"dependent":1,
"name":"Google",
"depLabel":"compound"
}
],
"isRoot":false,
"immediate_parent_id":3,
"parentVerbID":3
},
{
"node_id":3,
"token":"is",
"posTag":"VBZ",
"depLabel":"root",
"node_role":{
"parent_verb_id":3
},
"verb_info":[
{
"is_verb":true,
"nodeID":3,
"negation":null,
"lemma":"be",
"id":"be-109-1-1",
"pbID":"be.01",
"meaningUsage":"copula",
"verb_synonyms":[
"be",
"prove",
"appear",
"seem"
]
}
],
"action_verb_roleset":"",
"parent_node":{
"parent_node":-1,
"name":null
},
"entity_tag":null,
"siblings":[
],
"dependents":[
{
"dependent":2,
"name":"News",
"depLabel":"nsubj"
},
{
"dependent":1,
"name":"Google",
"depLabel":"compound"
},
{
"dependent":6,
"name":"aggregator",
"depLabel":"attr"
},
{
"dependent":4,
"name":"a",
"depLabel":"det"
},
{
"dependent":5,
"name":"news",
"depLabel":"compound"
},
{
"dependent":7,
"name":"and",
"depLabel":"cc"
},
{
"dependent":8,
"name":"app",
"depLabel":"conj"
},
{
"dependent":9,
"name":"developed",
"depLabel":"acl"
},
{
"dependent":10,
"name":"by",
"depLabel":"agent"
},
{
"dependent":11,
"name":"Google",
"depLabel":"pobj"
},
{
"dependent":12,
"name":".",
"depLabel":"punct"
}
],
"pobj_dependents":[
{
"dependent":11,
"name":"Google",
"depLabel":"pobj"
}
],
"dobj_dependents":[
],
"nobj_dependents":[
],
"parents":[
],
"immediate_dependents":[
{
"dependent":2,
"name":"News",
"depLabel":"nsubj"
},
{
"dependent":6,
"name":"aggregator",
"depLabel":"attr"
},
{
"dependent":12,
"name":".",
"depLabel":"punct"
}
],
"isRoot":true,
"immediate_parent_id":-1,
"parentVerbID":-1
},
{
"node_id":4,
"token":"a",
"posTag":"DT",
"depLabel":"det",
"node_role":{
"role":"comment"
},
"verb_info":[
],
"action_verb_roleset":"",
"parent_node":{
"parent_node":6,
"name":"aggregator"
},
"entity_tag":null,
"siblings":[
5,
7,
8,
9
],
"dependents":[
],
"pobj_dependents":[
],
"dobj_dependents":[
],
"nobj_dependents":[
],
"parents":[
6,
3
],
"immediate_dependents":[
],
"isRoot":false,
"immediate_parent_id":6,
"parentVerbID":3
},
{
"node_id":5,
"token":"news",
"posTag":"NN",
"depLabel":"compound",
"node_role":{
"role":"comment"
},
"verb_info":[
],
"action_verb_roleset":"",
"parent_node":{
"parent_node":6,
"name":"aggregator"
},
"entity_tag":null,
"siblings":[
4,
7,
8,
9
],
"dependents":[
],
"pobj_dependents":[
],
"dobj_dependents":[
],
"nobj_dependents":[
],
"parents":[
6,
3
],
"immediate_dependents":[
],
"isRoot":false,
"immediate_parent_id":6,
"parentVerbID":3
},
{
"node_id":6,
"token":"aggregator",
"posTag":"NN",
"depLabel":"attr",
"node_role":{
"parent_verb_id":3
},
"verb_info":[
],
"action_verb_roleset":"be.01",
"parent_node":{
"parent_node":3,
"name":"is"
},
"entity_tag":null,
"siblings":[
2,
12
],
"dependents":[
{
"dependent":4,
"name":"a",
"depLabel":"det"
},
{
"dependent":5,
"name":"news",
"depLabel":"compound"
},
{
"dependent":7,
"name":"and",
"depLabel":"cc"
},
{
"dependent":8,
"name":"app",
"depLabel":"conj"
},
{
"dependent":9,
"name":"developed",
"depLabel":"acl"
},
{
"dependent":10,
"name":"by",
"depLabel":"agent"
},
{
"dependent":11,
"name":"Google",
"depLabel":"pobj"
}
],
"pobj_dependents":[
{
"dependent":11,
"name":"Google",
"depLabel":"pobj"
}
],
"dobj_dependents":[
],
"nobj_dependents":[
],
"parents":[
3
],
"immediate_dependents":[
{
"dependent":4,
"name":"a",
"depLabel":"det"
},
{
"dependent":5,
"name":"news",
"depLabel":"compound"
},
{
"dependent":7,
"name":"and",
"depLabel":"cc"
},
{
"dependent":8,
"name":"app",
"depLabel":"conj"
},
{
"dependent":9,
"name":"developed",
"depLabel":"acl"
}
],
"isRoot":false,
"immediate_parent_id":3,
"parentVerbID":3
},
{
"node_id":7,
"token":"and",
"posTag":"CC",
"depLabel":"cc",
"node_role":{
"role":"comment"
},
"verb_info":[
],
"action_verb_roleset":"",
"parent_node":{
"parent_node":6,
"name":"aggregator"
},
"entity_tag":null,
"siblings":[
4,
5,
8,
9
],
"dependents":[
],
"pobj_dependents":[
],
"dobj_dependents":[
],
"nobj_dependents":[
],
"parents":[
6,
3
],
"immediate_dependents":[
],
"isRoot":false,
"immediate_parent_id":6,
"parentVerbID":3
},
{
"node_id":8,
"token":"app",
"posTag":"NN",
"depLabel":"conj",
"node_role":{
"role":"comment"
},
"verb_info":[
],
"action_verb_roleset":"",
"parent_node":{
"parent_node":6,
"name":"aggregator"
},
"entity_tag":null,
"siblings":[
4,
5,
7,
9
],
"dependents":[
],
"pobj_dependents":[
],
"dobj_dependents":[
],
"nobj_dependents":[
],
"parents":[
6,
3
],
"immediate_dependents":[
],
"isRoot":false,
"immediate_parent_id":6,
"parentVerbID":3
},
{
"node_id":9,
"token":"developed",
"posTag":"VBN",
"depLabel":"acl",
"node_role":{
"parent_verb_id":9,
"role":"comment"
},
"verb_info":[
{
"is_verb":true,
"nodeID":9,
"negation":null,
"lemma":"develop",
"id":"develop-26.1",
"pbID":"develop.02",
"meaningUsage":"create",
"verb_synonyms":[
"sculpt",
"cut",
"remake",
"shape",
"make",
"carve",
"build"
]
}
],
"action_verb_roleset":"",
"parent_node":{
"parent_node":6,
"name":"aggregator"
},
"entity_tag":null,
"siblings":[
4,
5,
7,
8
],
"dependents":[
{
"dependent":10,
"name":"by",
"depLabel":"agent"
},
{
"dependent":11,
"name":"Google",
"depLabel":"pobj"
}
],
"pobj_dependents":[
{
"dependent":11,
"name":"Google",
"depLabel":"pobj"
}
],
"dobj_dependents":[
],
"nobj_dependents":[
],
"parents":[
6,
3
],
"immediate_dependents":[
{
"dependent":10,
"name":"by",
"depLabel":"agent"
}
],
"isRoot":false,
"immediate_parent_id":6,
"parentVerbID":-1
},
{
"node_id":10,
"token":"by",
"posTag":"IN",
"depLabel":"agent",
"node_role":{
"parent_verb_id":9,
"grandparent_verb_id":9,
"role":"creator"
},
"verb_info":[
],
"action_verb_roleset":"develop.02",
"parent_node":{
"parent_node":9,
"name":"developed"
},
"entity_tag":null,
"siblings":[
],
"dependents":[
{
"dependent":11,
"name":"Google",
"depLabel":"pobj"
}
],
"pobj_dependents":[
{
"dependent":11,
"name":"Google",
"depLabel":"pobj"
}
],
"dobj_dependents":[
],
"nobj_dependents":[
],
"parents":[
9,
6,
3
],
"immediate_dependents":[
{
"dependent":11,
"name":"Google",
"depLabel":"pobj"
}
],
"isRoot":false,
"immediate_parent_id":9,
"parentVerbID":9
},
{
"node_id":11,
"token":"Google",
"posTag":"NNP",
"depLabel":"pobj",
"node_role":{
},
"verb_info":[
],
"action_verb_roleset":"",
"parent_node":{
"parent_node":10,
"name":"by"
},
"entity_tag":null,
"siblings":[
],
"dependents":[
],
"pobj_dependents":[
],
"dobj_dependents":[
],
"nobj_dependents":[
],
"parents":[
10,
9,
6,
3
],
"immediate_dependents":[
],
"isRoot":false,
"immediate_parent_id":10,
"parentVerbID":9
}
]
示例JSON输出(用于渲染d3树)
{
"name":"is",
"children":[
{
"name":"news",
"children":[
{
"name":"Google",
"children":[
]
},
{
"name":"aggregator",
"children":[
{
"name":"a",
"children":[
]
},
{
"name":"news",
"children":[
]
},
{
"name":"app",
"children":[
]
},
{
"name":"developed",
"children":[
{
"name":"by"
}
]
}
]
},
{
"name":".",
"children":[
]
}
]
}
]
}
答案 0 :(得分:1)
您可以采用迭代方法,将所有已知信息,父项和子项存储在一个对象中,然后使根子项成为肥胖子项。
var data = [{ node_id: 1, token: "Google", parent_node: { parent_node: 2, name: "News" } }, { node_id: 2, token: "News", parent_node: { parent_node: 3, name: "is" } }, { node_id: 3, token: "is", parent_node: { parent_node: -1, name: null } }, { node_id: 4, token: "a", parent_node: { parent_node: 6, name: "aggregator" } }, { node_id: 5, token: "news", parent_node: { parent_node: 6, name: "aggregator" } }, { node_id: 6, token: "aggregator", parent_node: { parent_node: 3, name: "is" } }, { node_id: 7, token: "and", parent_node: { parent_node: 6, name: "aggregator" } }, { node_id: 8, token: "app", parent_node: { parent_node: 6, name: "aggregator" } }, { node_id: 9, token: "developed", parent_node: { parent_node: 6, name: "aggregator" } }, { node_id: 10, token: "by", parent_node: { parent_node: 9, name: "developed" } }, { node_id: 11, token: "Google", parent_node: { parent_node: 10, name: "by" } }],
tree = function (data, root) {
var o = {};
data.forEach(function ({ node_id: id, token: name, parent_node: { parent_node: parent } }) {
var temp = { name };
if (o[id] && o[id].children) {
temp.children = o[id].children;
}
o[id] = temp;
o[parent] = o[parent] || {};
o[parent].children = o[parent].children || [];
o[parent].children.push(temp);
});
return o[root].children;
}(data, -1);
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }