将递归的 JSON 对象转换为另一个 JSON 对象。

3
需要帮助将递归的JSON对象转换为另一个JSON对象。
我的输入对象如下所示:
{  
  person: 
  {
       name: 'John Henry',
       age: 63
  },
  children: 
  [
        {
            person:
            {
                name: 'Paul Henry',
                age: 40
            },
            children: 
            [
                {
                    person:
                    {
                        name: 'Tom Henry',
                        age: 10
                    }
                }
                {
                    person:
                    {
                        name: 'Mike Henry',
                        age: 12
                    }
                }
            ]
        },
        {
            person:
            {
                name: 'Wilson Henry',
                age: 30
            }
        },
        {
            person:
            {
                name: 'Richard Henry',
                age: 59
            }
        }
  ]
}

我需要的输出是:
[{
          name: 'John Henry',
          attributes: { age: 63 },
          children: 
          [
            {
                name: 'Paul Henry',
                attributes: { age: 40 },
                children: [
                  {
                      name: 'Tom Henry',
                      attributes: { age: 10 }
                  },
                  {
                      name: 'Mike Henry',
                      attributes: { age: 12 }
                  }
                ]
            },
            {
                name: 'Wilson Henry',
                attributes: { age: 30 }
            },
            {
                name: 'Richard Henry',
                attributes: { age: 59 }
            }
         ]
}];

这是我迄今为止尝试过的内容,但在递归部分卡住了。不确定如何将所有东西结合起来。
var tree = {};
var getFamilyTree = function (input) {

  tree.name = input.person.name;
  tree.attributes = { 'age': input.person.age };
  tree.children = [];

  input.children.forEach(function (child) {
      //not sure how to recursively go through the child nodes.
  });
};
1个回答

3
在函数开始时创建一个新的输出对象。
对于每个子元素,再次调用getFamilyTree()方法,并将其作为新的input。将该调用的结果附加到您的新子元素列表中。
返回您新创建的对象。
类似这样:
function getFamilyTree(input)
{
  var newb = { 'attributes': {} };

  for ( f in input.person )    // we may have attributes other than "age"
    if (f == 'name')
      newb.name = input.person[f];
    else
      newb.attributes[f] = input.person[f];

  if (input.children && input.children.length)
    {
      newb.children = [];

      for ( var i = 0; i < input.children.length; ++i )
        {
          newb.children.push(getFamilyTree(input.children[i]));
        }
    }

  return newb;
}

var orig = {  
  person: 
  {
       name: 'John Henry',
       age: 63
  },
  children: 
  [
        {
            person:
            {
                name: 'Paul Henry',
                age: 40
            },
            children: 
            [
                {
                    person:
                    {
                        name: 'Tom Henry',
                        age: 10
                    }
                },
                {
                    person:
                    {
                        name: 'Mike Henry',
                        age: 12
                    }
                }
            ]
        },
        {
            person:
            {
                name: 'Wilson Henry',
                age: 30
            }
        },
        {
            person:
            {
                name: 'Richard Henry',
                age: 59
            }
        }
  ]
};


function getFamilyTree(o)
{
  var newb = { 'attributes': {} };
  
  for ( f in o.person )    // we may have attributes other than "age"
    if (f == 'name')
      newb.name = o.person[f];
    else
      newb.attributes[f] = o.person[f];
  
  if (o.children && o.children.length)
    {
      newb.children = [];
      
      for ( var i = 0; i < o.children.length; ++i )
        {
          newb.children.push(getFamilyTree(o.children[i]));
        }
    }
  
  return newb;
}


console.log( JSON.stringify( getFamilyTree(orig), null, "  " ) );


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接