当前位置: 首页 > 面试题库 >

将平面JSON文件转换为分层的json数据,例如flare.json [d3示例文件]

孔深
2023-03-14
问题内容

经过一番麻烦的战斗之后,我几乎想出了如何将平面json文件转换为Hierarchical文件。我不是自己写函数的。

但是现在的问题是,在帖子中编写的函数只有两个层次结构。但是我正在寻找4个层次的层次结构。我尝试覆盖失败的功能,但是。

用我正在尝试的代码。


         var data = [
        { "dep": "First Top", "name": "First child", "model": "value1", "size": "320" },
        { "dep": "First Top", "name": "First child", "model": "value2", "size": "320" },
        { "dep": "First Top", "name": "First child", "model": "value3", "size": "320" },
        { "dep": "First Top", "name": "First child", "model": "value4", "size": "320" },
        { "dep": "First Top", "name": "SECOND CHILD", "model": "value1", "size": "320" },
        { "dep": "First Top", "name": "SECOND CHILD", "model": "value2", "size": "320" },
        { "dep": "First Top", "name": "SECOND CHILD", "model": "value3", "size": "320" },
        { "dep": "First Top", "name": "SECOND CHILD", "model": "value4", "size": "320" },
        { "dep": "Second Top", "name": "First Child", "model": "value1", "size": "320" },
        { "dep": "Second Top", "name": "First Child", "model": "value2", "size": "320" },
        { "dep": "Second Top", "name": "First Child", "model": "value3", "size": "320" },
        { "dep": "Second Top", "name": "First Child", "model": "value4", "size": "320" },
        { "dep": "Second Top", "name": "SECOND CHILD", "model": "value1", "size": "320" },
        { "dep": "Second Top", "name": "SECOND CHILD", "model": "value2", "size": "320" },
        { "dep": "Second Top", "name": "SECOND CHILD", "model": "value3", "size": "320" },
        { "dep": "Second Top", "name": "SECOND CHILD", "model": "value4", "size": "320" },
        { "dep": "Third Top", "name": "First Child", "model": "value2", "size": "320" },
        { "dep": "Third Top", "name": "First Child", "model": "value3", "size": "320" },
        { "dep": "Third Top", "name": "First Child", "model": "value4", "size": "320" },
        { "dep": "Third Top", "name": "First Child", "model": "value5", "size": "320" },
        { "dep": "Third Top", "name": "Second Child", "model": "value1", "size": "320" },
        { "dep": "Third Top", "name": "Second Child", "model": "value2", "size": "320" },
        { "dep": "Third Top", "name": "Second Child", "model": "value3", "size": "320" },
        { "dep": "Third Top", "name": "Second Child", "model": "value4", "size": "320" }
      ]

    var newData = {"name":"root", "children":{}}

    data.forEach(function(d){
        if(typeof newData.children[d.dep] !== 'undefined')  {
            newData.children[d.dep].children.push(d)
        } else {
            newData.children[d.dep] = {"name": d.dep, "children": [{"name": d.name, "children": [{"name": d.model, "size": d.size}]}]}
        }
    })





    newData.children = Object.keys(newData.children).map(function (key) { return newData.children[key]; });

              // show what we've got
              d3.select('body').append('pre')
                  .text(JSON.stringify(newData, null, '  '));


当前代码的输出

```

{
  "name": "root",
  "children": [
    {
      "name": "First Top",
      "children": [
        {
          "name": "First child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            }
          ]
        },
        {
          "dep": "First Top",
          "name": "First child",
          "model": "value2",
          "size": "320"
        },
        {
          "dep": "First Top",
          "name": "First child",
          "model": "value3",
          "size": "320"
        },
        {
          "dep": "First Top",
          "name": "First child",
          "model": "value4",
          "size": "320"
        },
        {
          "dep": "First Top",
          "name": "SECOND CHILD",
          "model": "value1",
          "size": "320"
        },
        {
          "dep": "First Top",
          "name": "SECOND CHILD",
          "model": "value2",
          "size": "320"
        },
        {
          "dep": "First Top",
          "name": "SECOND CHILD",
          "model": "value3",
          "size": "320"
        },
        {
          "dep": "First Top",
          "name": "SECOND CHILD",
          "model": "value4",
          "size": "320"
        }
      ]
    },
    {
      "name": "Second Top",
      "children": [
        {
          "name": "First Child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            }
          ]
        },
        {
          "dep": "Second Top",
          "name": "First Child",
          "model": "value2",
          "size": "320"
        },
        {
          "dep": "Second Top",
          "name": "First Child",
          "model": "value3",
          "size": "320"
        },
        {
          "dep": "Second Top",
          "name": "First Child",
          "model": "value4",
          "size": "320"
        },
        {
          "dep": "Second Top",
          "name": "SECOND CHILD",
          "model": "value1",
          "size": "320"
        },
        {
          "dep": "Second Top",
          "name": "SECOND CHILD",
          "model": "value2",
          "size": "320"
        },
        {
          "dep": "Second Top",
          "name": "SECOND CHILD",
          "model": "value3",
          "size": "320"
        },
        {
          "dep": "Second Top",
          "name": "SECOND CHILD",
          "model": "value4",
          "size": "320"
        }
      ]
    },
    {
      "name": "Third Top",
      "children": [
        {
          "name": "First Child",
          "children": [
            {
              "name": "value2",
              "size": "320"
            }
          ]
        },
        {
          "dep": "Third Top",
          "name": "First Child",
          "model": "value3",
          "size": "320"
        },
        {
          "dep": "Third Top",
          "name": "First Child",
          "model": "value4",
          "size": "320"
        },
        {
          "dep": "Third Top",
          "name": "First Child",
          "model": "value5",
          "size": "320"
        },
        {
          "dep": "Third Top",
          "name": "Second Child",
          "model": "value1",
          "size": "320"
        },
        {
          "dep": "Third Top",
          "name": "Second Child",
          "model": "value2",
          "size": "320"
        },
        {
          "dep": "Third Top",
          "name": "Second Child",
          "model": "value3",
          "size": "320"
        },
        {
          "dep": "Third Top",
          "name": "Second Child",
          "model": "value4",
          "size": "320"
        }
      ]
    }
  ]
}

```

所需的输出格式:

```

{
  "name": "root",
  "children": [
    {
      "name": "First Top",
      "children": [
        {
          "name": "First child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            },
            {
              "name": "value2",
              "size": "320"
            },
            {
              "name": "value3",
              "size": "320"
            },
            {
              "name": "value4",
              "size": "320"
            }
          ]
        },
        {
          "name": "Second child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            },
            {
              "name": "value2",
              "size": "320"
            },
            {
              "name": "value3",
              "size": "320"
            },
            {
              "name": "value4",
              "size": "320"
            }
          ]
        },

      ]
    },
    {
      "name": "Second Top",
      "children": [
        {
          "name": "First child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            },
            {
              "name": "value2",
              "size": "320"
            },
            {
              "name": "value3",
              "size": "320"
            },
            {
              "name": "value4",
              "size": "320"
            }
          ]
        },
        {
          "name": "Second child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            },
            {
              "name": "value2",
              "size": "320"
            },
            {
              "name": "value3",
              "size": "320"
            },
            {
              "name": "value4",
              "size": "320"
            }
          ]
        },
      ]
    },
    {
      "name": "Third Top",
      "children": [
        {
          "name": "First child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            },
            {
              "name": "value2",
              "size": "320"
            },
            {
              "name": "value3",
              "size": "320"
            },
            {
              "name": "value4",
              "size": "320"
            }
          ]
        },
        {
          "name": "Second child",
          "children": [
            {
              "name": "value1",
              "size": "320"
            },
            {
              "name": "value2",
              "size": "320"
            },
            {
              "name": "value3",
              "size": "320"
            },
            {
              "name": "value4",
              "size": "320"
            }
          ]
        },
      ]
    }
  ]
}

```

我一个星期都在crack头,但我一个人也搞不清。有人请修改此功能,以便在更新后以分层格式获取数据。

提前致谢!!


问题答案:

更新为使用递归方法

这应该适用于n级别,而不只是2或3。您只需要指定哪些属性定义哪些级别即可。

var data = [
    { "dep": "First Top", "name": "First child", "model": "value1", "size": "320" },
    { "dep": "First Top", "name": "First child", "model": "value2", "size": "320" },
    { "dep": "First Top", "name": "SECOND CHILD", "model": "value1", "size": "320" },
    { "dep": "Second Top", "name": "First Child", "model": "value1", "size": "320" }
];

var newData = { name :"root", children : [] },
    levels = ["dep","name"];

// For each data row, loop through the expected levels traversing the output tree
data.forEach(function(d){
    // Keep this as a reference to the current level
    var depthCursor = newData.children;
    // Go down one level at a time
    levels.forEach(function( property, depth ){

        // Look to see if a branch has already been created
        var index;
        depthCursor.forEach(function(child,i){
            if ( d[property] == child.name ) index = i;
        });
        // Add a branch if it isn't there
        if ( isNaN(index) ) {
            depthCursor.push({ name : d[property], children : []});
            index = depthCursor.length - 1;
        }
        // Now reference the new child array as we go deeper into the tree
        depthCursor = depthCursor[index].children;
        // This is a leaf, so add the last element to the specified branch
        if ( depth === levels.length - 1 ) depthCursor.push({ name : d.model, size : d.size });
    });
});


 类似资料:
  • 经过一场麻烦的战斗,我几乎想出了如何将平面json文件转换为层次化的json文件。我不是自己写的函数。我从下面的帖子上抄下来的。 一个星期以来,我一直在发疯,但我自己也想不出来。有人请修改功能,以获得数据的层次格式,因为我已经更新。 提前感谢!!

  • 问题内容: 我如何将JSON文件这样转换为数据框以进行一些转换。 例如,如果JSON文件读取: 我如何将其转换为这样的表 问题答案: 从字典对象创建数据框。 如果您有嵌套的列,那么您首先需要规范化数据: 来源:https : //github.com/vi3k6i5/pandas_basics/blob/master/1_a_create_a_dataframe_from_dictonary.ip

  • 我遇到了一个问题,使用颠簸转换将平面 JSON 转换为嵌套 JSON。而且我对颠簸转型很陌生。输入和输出详细信息如下。 我的输入: 预期产量

  • 我是Java新手,我试图在不使用任何外部库的情况下将任何给定的json文件转换为html文件,除了GSON来解释json结构。由于我目前还不知道如何正确处理这个问题,我想谦卑地请求您的帮助。 这是一个示例json文件: 这是文件的预期输出: 这是我目前拥有的代码:(实际的转换方法) (使用GSON的json提取方法) 这是我当前对html文件的输出,显然是不正确的:

  • 输入 json : 预期输出: 我想有一个颠簸转换,它可以嵌套很少的田地。

  • 问题内容: 这是我的代码,非常简单的东西… 声明一些字段名称,阅读器使用CSV读取文件,并使用字段名称将文件转储为JSON格式。这是问题所在… CSV文件中的每个记录都在不同的行上。我希望JSON输出采用相同的方式。问题在于,将其全部倾倒在一条长长的巨线上。 我试过使用类似的代码,然后在该代码下面运行我的代码,该代码循环遍历每一行,但它在一行上执行整个文件,然后在另一行上遍历整个文件…一直持续到行