建设性地操作未知深度的 JSON 树中的任何值/对象
我有一个包含节点和子节点的 JSON 树 - 格式为:
jsonObject =
{
id:nodeid_1,
children: [
{
id:nodeid_2,
children:[]
},
{
id:nodeid_3,
children:[
{
id:nodeid_4,
children:[]
},
{
id:nodeid_5,
children:[]
}
}
}
我不知道这棵树的深度,一个节点能够拥有许多子节点,这些子节点也有许多子节点,依此类推。
我的问题是我需要使用 nodeID 将节点添加到这棵树中。例如,一个可以采用 nodeID 和节点对象(包括其子对象)的函数将能够替换树中的该节点 - 结果将成为一棵更大的树。
我只遇到过允许我遍历 JSON 树中所有节点的递归函数,并且我对其中一个函数所做的修改返回了节点对象 - 但对我没有帮助,因为我需要修改原始树:
var findNode = {
node:{},
find:function(nodeID,jsonObj) {
if( typeof jsonObj == "object" ) {
$.each(jsonObj, function(k,v) {
if(v == nodeID) {
findNode.node = $(jsonObj).eq(0).toArray()[0];
} else {
findNode.find(nodeID,v);
}
});
} else {
//console.log("jsobObj is not an object");
}
}
}
这允许我进行以下测试:
findNode.find("nodeid_3",json);
alert(findNode.node);
总结一下 - 如何修改深度未知的 JSON 树的值?
提前致谢
I have a JSON tree that contains nodes and children - the format is:
jsonObject =
{
id:nodeid_1,
children: [
{
id:nodeid_2,
children:[]
},
{
id:nodeid_3,
children:[
{
id:nodeid_4,
children:[]
},
{
id:nodeid_5,
children:[]
}
}
}
I don't know the depth of this tree, a node is capable of having many children that also have many children and so on.
My problem is that I need to add nodes into this tree by using a nodeID. For example, a function that can take a nodeID and the node object (including its children), would be able to replace that node within the tree - which as a result would become a bigger tree.
I have only come across recursive functions that allow me to traverse all the nodes within a JSON tree and a modification I have made of one of these functions returns me the node object - but doesn't help me as I need to modify the original tree:
var findNode = {
node:{},
find:function(nodeID,jsonObj) {
if( typeof jsonObj == "object" ) {
$.each(jsonObj, function(k,v) {
if(v == nodeID) {
findNode.node = $(jsonObj).eq(0).toArray()[0];
} else {
findNode.find(nodeID,v);
}
});
} else {
//console.log("jsobObj is not an object");
}
}
}
which allows me to do the following test:
findNode.find("nodeid_3",json);
alert(findNode.node);
So to sum up - how can I modify a value of a JSON tree that has an unknown depth?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想修改一个节点,就像你说的,你可以只修改该节点的属性。
另外,你为什么要使用 jQuery 来实现这个目的?
这是一个不使用 jQuery 的替代方案:
If you want to modify a node, like you said, you can just modify the properties of that node.
Also, why are you using jQuery for this?
Here's an alternative without using jQuery should work:
那不是 JSON,它是一个 Javascript 对象文字。 JSON 是一种将简单的 Javascript 对象编码为字符串的特定方法;你的例子不是这样写的。此外,您的代码不会操作 JSON 对象(实际上是字符串,而不是对象);它操作 Javascript 对象(这是一个更简单的任务)。
也就是说,我不确定您的实际问题是什么,但如果是向
children
数组添加新元素,您可以使用 Array.push:(这假设 findNode.find 确实有效,但我很确定它不会。)
That is not JSON, it is a Javascript object literal. JSON is a certain way to encode a simple Javascript object into a string; your example is not written that way. Also, your code does not manipulate JSON objects (which are actually strings, not objects); it manipulates Javascript objects (which is a way simpler task).
That said, I'm not sure what your actual question is, but if it's about adding new elements to the
children
array, you can use Array.push:(This assumes that findNode.find actually works, which I'm pretty sure it does not.)