建设性地操作未知深度的 JSON 树中的任何值/对象

发布于 2024-09-19 16:45:13 字数 1095 浏览 4 评论 0原文

我有一个包含节点和子节点的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你的手,一向走下去 2024-09-26 16:45:13

如果你想修改一个节点,就像你说的,你可以只修改该节点的属性。

var node = findNode.find("nodeid_3",json);
node.id = "nodeid_3_modified";
node.children = [];

另外,你为什么要使用 jQuery 来实现这个目的?

这是一个不使用 jQuery 的替代方案:

function findNode(object, nodeId) {
   if (object.id === nodeId) return object;

   var result;
   for (var i = 0; i < object.children.length; i++) {
      result = findNode(object.children[i], nodeId);
      if (result !== undefined) return result;
   }
}

If you want to modify a node, like you said, you can just modify the properties of that node.

var node = findNode.find("nodeid_3",json);
node.id = "nodeid_3_modified";
node.children = [];

Also, why are you using jQuery for this?

Here's an alternative without using jQuery should work:

function findNode(object, nodeId) {
   if (object.id === nodeId) return object;

   var result;
   for (var i = 0; i < object.children.length; i++) {
      result = findNode(object.children[i], nodeId);
      if (result !== undefined) return result;
   }
}
染年凉城似染瑾 2024-09-26 16:45:13

那不是 JSON,它是一个 Javascript 对象文字。 JSON 是一种将简单的 Javascript 对象编码为字符串的特定方法;你的例子不是这样写的。此外,您的代码不会操作 JSON 对象(实际上是字符串,而不是对象);它操作 Javascript 对象(这是一个更简单的任务)。

也就是说,我不确定您的实际问题是什么,但如果是向 children 数组添加新元素,您可以使用 Array.push:(

findNode.find("nodeid_3",json);
findNode.node.children.push(child);

这假设 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:

findNode.find("nodeid_3",json);
findNode.node.children.push(child);

(This assumes that findNode.find actually works, which I'm pretty sure it does not.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文