如何将JSON转换为另一个

时间:2017-03-20 21:41:55

标签: javascript php json typescript

我以这种方式从Laravel API接收JSON:

[  
   {  
      "id":48,
      "parentid":0,
      "title":"Item 1",
      "child_content":[  
         {  
            "id":49,
            "parentid":48,
            "title":"Itema 1.1",
         },
         {  
            "id":52,
            "parentid":48,
            "title":"Item 1.2",
         }
      ]
   },
   {  
      "id":58,
      "parentid":0,
      "title":"Item 2",
      "child_content":[  
         {  
            "id":59,
            "parentid":58,
            "title":"Itema 2.1",
         },
         {  
            "id":60,
            "parentid":58,
            "title":"Item 2.2",
         }
      ]
   } 
]

我需要的是将JSON改为:

{
    "data":
    [
        {
            "data":
            {
                "id":68,
                "parentid":0,
                "title":"Item 1"
            },
            "children":
            [
                {
                    "data":
                    {
                        "id":69,
                        "parentid":68,
                        "title":"Item 1.1"
                    },
                },
                {
                    "data":
                    {
                        "id":69,
                        "parentid":68,
                        "title":"Item 1.2"
                    }
                }
            ]
        }
    ]
}

我一直在处理这个......但是我无法找到正确的方法...

如何在PHP或Javascript / TypeScript(Angular 2)中执行此操作。

提前谢谢。

5 个答案:

答案 0 :(得分:1)

这应该达到你的目标。基本上我只是抓住child_content,将其重命名为children并复制其他3个属性。 children.map次迭代将现有数据放入一个关键字为data的对象中:

const input = [{"id":48,"parentid":0,"title":"Item 1","child_content":[{"id":49,"parentid":48,"title":"Itema 1.1"},{"id":52,"parentid":48,"title":"Item 1.2"}]},{"id":58,"parentid":0,"title":"Item 2","child_content":[{"id":59,"parentid":58,"title":"Itema 2.1"},{"id":60,"parentid":58,"title":"Item 2.2"}]}]

const output = {
   data: input.map((data) => {
      const { 
        child_content: children,
        id,
        parentId,
        title,
      } = data;

      return {
         id,
         parentId,
         title,
         children: children.map(data => ({data})),
      };
   })
}

console.log(output);

答案 1 :(得分:0)

假设两个数据集的差异是由于来自不同数据集的复制/粘贴,您可以使用数组map方法为您转换数据。

map通过遍历数组的每个项目来工作,并允许您返回您喜欢的形状的新项目。



var input = [{"id":48,"parentid":0,"title":"Item 1","child_content":[{"id":49,"parentid":48,"title":"Itema 1.1"},{"id":52,"parentid":48,"title":"Item 1.2"}]},{"id":58,"parentid":0,"title":"Item 2","child_content":[{"id":59,"parentid":58,"title":"Itema 2.1"},{"id":60,"parentid":58,"title":"Item 2.2"}]}];

var output = {
  data: input.map(function(parent) {
    // return a new object which contains the properties which you need
    return {
      data: {
        id: parent.id,
        parentid: parent.parentid,
        title: parent.title
      },
      // children is an array, so we can use map again to transform them
      children: parent.child_content.map(function(child) {
        return {
          data: {
            id: child.id,
            parentid: parent.id,
            title: child.title
          }
        };
      })
    }
  })
}

console.log(output);




答案 2 :(得分:0)

您可以在不使用convert函数的迭代和递归调用来改变原始对象的情况下转换结构。

适用于任何深度。

function convert(o) {
    var temp = { data: {} };
    Object.keys(o).forEach(function (k) {
        if (k === 'child_content') {
            temp.children = o[k].map(convert);
        } else {
            temp.data[k] = o[k];
        }
    });
    return temp;
}

var data = [{ id: 48, parentid: 0, title: "Item 1", child_content: [{ id: 49, parentid: 48, title: "Itema 1.1" }, { id: 52, parentid: 48, title: "Item 1.2" }] }, { id: 58, parentid: 0, title: "Item 2", child_content: [{ id: 59, parentid: 58, title: "Itema 2.1" }, { id: 60, parentid: 58, title: "Item 2.2" }] }],
    result = { data: data.map(convert) };

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:0)

您可以使用JavaScript Array.prototype.map()

var json = [{"id": 48,"parentid": 0,"title": "Item 1","child_content": [{"id": 49,"parentid": 48,"title": "Itema 1.1",}, {"id": 52,"parentid": 48,"title": "Item 1.2",}]}, {"id": 58,"parentid": 0,"title": "Item 2","child_content": [{"id": 59,"parentid": 58,"title": "Itema 2.1",}, {"id": 60,"parentid": 58,"title": "Item 2.2",}]}],
    result = {
      data: json.map(function (item) {
        return {
          data: {
            id: item.id,
            parentid: item.parentid,
            title: item.title
          },
          children: item.child_content.map(function (childItem) {
            return {
              data: {
                id: childItem.id,
                parentid: childItem.parentid,
                title: childItem.title
              }
            }
          })
        };
      })
    };

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 4 :(得分:0)

好吧,我最终做的可能不是太优雅......但它有效,可能我应该创建一个递归函数来管理不同的嵌套级别。我采用了@RobM解决方案并在第二级复制了子功能,如下所示:

convert(input)
{
    const output =
    {
       data: input.map((data) =>
       {
          const { children: children } = data;
          delete data.children;
          return {
             data,
             children: children.map(data =>
             {
                const { children: children } = data;
                delete data.children;
                return {
                    data,
                    children: children.map(data => ({data})),
                };
            }),
          };
       })
    }

    return output.data;
}