Frage Konvertiere flache JSON-Datei in hierarchische JSON-Daten wie flare.json [d3 Beispieldatei]


Nach einem mühsamen Kampf dachte ich fast, wie man eine flache JSON-Datei in eine hierarchische konvertieren könnte. Ich habe die Funktion nicht selbst geschrieben. Ich habe es von unten nachgeschrieben.

D3 JSON DATENKONVERTIERUNG

Aber jetzt ist das Problem jetzt, die Funktion, die in der Post geschrieben wurde, hat nur 2 Ebenen der Hierarchie. Aber ich suche nach 4 Ebenen Hierarchie. Ich habe versucht, die Funktion außer Kraft zu setzen, bei der ich gescheitert bin.

Code mit dem, was ich versuche.


    
         var daten = [
        {"dep": "Erster Anfang", "Name": "Erstes Kind", "Modell": "Wert1", "Größe": "320"},
        {"dep": "First Top", "Name": "erstes Kind", "Modell": "value2", "size": "320"},
        {"dep": "Erster Anfang", "Name": "Erstes Kind", "Modell": "Wert3", "Größe": "320"},
        {"dep": "Erster Anfang", "Name": "Erstes Kind", "Modell": "Wert4", "Größe": "320"},
        {"dep": "Erster Anfang", "Name": "ZWEITES KIND", "Modell": "Wert1", "Größe": "320"},
        {"dep": "Erster Anfang", "Name": "ZWEITES KIND", "Modell": "Wert2", "Größe": "320"},
        {"dep": "Erster Anfang", "Name": "ZWEITES KIND", "Modell": "Wert3", "Größe": "320"},
        {"dep": "First Top", "Name": "ZWEITES KIND", "Modell": "value4", "size": "320"},
        {"dep": "Zweites Top", "Name": "Erstes Kind", "Modell": "Wert1", "Größe": "320"},
        {"dep": "Zweites Top", "Name": "Erstes Kind", "Modell": "Wert2", "Größe": "320"},
        {"dep": "Zweites Top", "Name": "Erstes Kind", "Modell": "Wert3", "Größe": "320"},
        {"dep": "Zweites Top", "Name": "First Child", "Modell": "value4", "size": "320"},
        {"dep": "Zweites Oberteil", "Name": "ZWEITES KIND", "Modell": "Wert1", "Größe": "320"},
        {"dep": "Zweites Oberteil", "Name": "ZWEITES KIND", "Modell": "Wert2", "Größe": "320"},
        {"dep": "Zweites Top", "Name": "ZWEITES KIND", "Modell": "Wert3", "Größe": "320"},
        {"dep": "Zweites Top", "Name": "SECOND CHILD", "Modell": "value4", "size": "320"},
        {"dep": "Dritter Anfang", "Name": "Erstes Kind", "Modell": "Wert2", "Größe": "320"},
        {"dep": "Dritter Anfang", "Name": "Erstes Kind", "Modell": "Wert3", "Größe": "320"},
        {"dep": "Dritter Anfang", "Name": "Erstes Kind", "Modell": "Wert4", "Größe": "320"},
        {"dep": "Dritter Anfang", "Name": "Erstes Kind", "Modell": "Wert5", "Größe": "320"},
        {"dep": "Drittes Top", "Name": "Zweites Kind", "Modell": "Wert1", "Größe": "320"},
        {"dep": "Drittes Top", "Name": "Zweites Kind", "Modell": "Wert2", "Größe": "320"},
        {"dep": "Drittes Top", "Name": "Zweites Kind", "Modell": "Wert3", "Größe": "320"},
        {"dep": "Drittes Top", "Name": "Zweites Kind", "Modell": "value4", "size": "320"}
      ]

    var newData = {"Name": "root", "Kinder": {}}

    data.forEach (Funktion (d) {
        if (typeof newData.children [d.dep]! == 'undefiniert') {
            newData.children [d.dep] .children.push (d)
        } sonst {
            newData.children [d.dep] = {"Name": d.dep, "Kinder": [{"Name": d.Name, "Kinder": [{"Name": d.model, "Größe": d.Size}]}]}
        }
    })





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

              // zeig was wir haben
              d3.select ('Körper') append ('pre')
                  .text (JSON.stringify (newData, null, ''));
        

Ausgabe des aktuellen Codes


    {
      "Name": "Wurzel",
      "Kinder": [
        {
          "Name": "Erstes Top",
          "Kinder": [
            {
              "Name": "Erstes Kind",
              "Kinder": [
                {
                  "Name": "Wert1",
                  "Größe": "320"
                }
              ]
            },
            {
              "dep": "Erstes Top",
              "Name": "Erstes Kind",
              "Modell": "Wert2",
              "Größe": "320"
            },
            {
              "dep": "Erstes Top",
              "Name": "Erstes Kind",
              "Modell": "Wert3",
              "Größe": "320"
            },
            {
              "dep": "Erstes Top",
              "Name": "Erstes Kind",
              "Modell": "Wert4",
              "Größe": "320"
            },
            {
              "dep": "Erstes Top",
              "Name": "ZWEITES KIND",
              "Modell": "Wert1",
              "Größe": "320"
            },
            {
              "dep": "Erstes Top",
              "Name": "ZWEITES KIND",
              "Modell": "Wert2",
              "Größe": "320"
            },
            {
              "dep": "Erstes Top",
              "Name": "ZWEITES KIND",
              "Modell": "Wert3",
              "Größe": "320"
            },
            {
              "dep": "Erstes Top",
              "Name": "ZWEITES KIND",
              "Modell": "Wert4",
              "Größe": "320"
            }
          ]
        },
        {
          "Name": "Zweites Top",
          "Kinder": [
            {
              "Name": "Erstes Kind",
              "Kinder": [
                {
                  "Name": "Wert1",
                  "Größe": "320"
                }
              ]
            },
            {
              "dep": "Zweites Top",
              "Name": "Erstes Kind",
              "Modell": "Wert2",
              "Größe": "320"
            },
            {
              "dep": "Zweites Top",
              "Name": "Erstes Kind",
              "Modell": "Wert3",
              "Größe": "320"
            },
            {
              "dep": "Zweites Top",
              "Name": "Erstes Kind",
              "Modell": "Wert4",
              "Größe": "320"
            },
            {
              "dep": "Zweites Top",
              "Name": "ZWEITES KIND",
              "Modell": "Wert1",
              "Größe": "320"
            },
            {
              "dep": "Zweites Top",
              "Name": "ZWEITES KIND",
              "Modell": "Wert2",
              "Größe": "320"
            },
            {
              "dep": "Zweites Top",
              "Name": "ZWEITES KIND",
              "Modell": "Wert3",
              "Größe": "320"
            },
            {
              "dep": "Zweites Top",
              "Name": "ZWEITES KIND",
              "Modell": "Wert4",
              "Größe": "320"
            }
          ]
        },
        {
          "Name": "Drittes Top",
          "Kinder": [
            {
              "Name": "Erstes Kind",
              "Kinder": [
                {
                  "Name": "Wert2",
                  "Größe": "320"
                }
              ]
            },
            {
              "dep": "Drittes Top",
              "Name": "Erstes Kind",
              "Modell": "Wert3",
              "Größe": "320"
            },
            {
              "dep": "Drittes Top",
              "Name": "Erstes Kind",
              "Modell": "Wert4",
              "Größe": "320"
            },
            {
              "dep": "Drittes Top",
              "Name": "Erstes Kind",
              "Modell": "Wert5",
              "Größe": "320"
            },
            {
              "dep": "Drittes Top",
              "Name": "Zweites Kind",
              "Modell": "Wert1",
              "Größe": "320"
            },
            {
              "dep": "Drittes Top",
              "Name": "Zweites Kind",
              "Modell": "Wert2",
              "Größe": "320"
            },
            {
              "dep": "Drittes Top",
              "Name": "Zweites Kind",
              "Modell": "Wert3",
              "Größe": "320"
            },
            {
              "dep": "Drittes Top",
              "Name": "Zweites Kind",
              "Modell": "Wert4",
              "Größe": "320"
            }
          ]
        }
      ]
    }

Gewünschtes Ausgabeformat:


    {
      "Name": "Wurzel",
      "Kinder": [
        {
          "Name": "Erstes Top",
          "Kinder": [
            {
              "Name": "Erstes Kind",
              "Kinder": [
                {
                  "Name": "Wert1",
                  "Größe": "320"
                },
                {
                  "Name": "Wert2",
                  "Größe": "320"
                },
                {
                  "Name": "Wert3",
                  "Größe": "320"
                },
                {
                  "Name": "Wert4",
                  "Größe": "320"
                }
              ]
            },
            {
              "Name": "Zweites Kind",
              "Kinder": [
                {
                  "Name": "Wert1",
                  "Größe": "320"
                },
                {
                  "Name": "Wert2",
                  "Größe": "320"
                },
                {
                  "Name": "Wert3",
                  "Größe": "320"
                },
                {
                  "Name": "Wert4",
                  "Größe": "320"
                }
              ]
            },

          ]
        },
        {
          "Name": "Zweites Top",
          "Kinder": [
            {
              "Name": "Erstes Kind",
              "Kinder": [
                {
                  "Name": "Wert1",
                  "Größe": "320"
                },
                {
                  "Name": "Wert2",
                  "Größe": "320"
                },
                {
                  "Name": "Wert3",
                  "Größe": "320"
                },
                {
                  "Name": "Wert4",
                  "Größe": "320"
                }
              ]
            },
            {
              "Name": "Zweites Kind",
              "Kinder": [
                {
                  "Name": "Wert1",
                  "Größe": "320"
                },
                {
                  "Name": "Wert2",
                  "Größe": "320"
                },
                {
                  "Name": "Wert3",
                  "Größe": "320"
                },
                {
                  "Name": "Wert4",
                  "Größe": "320"
                }
              ]
            },
          ]
        },
        {
          "Name": "Drittes Top",
          "Kinder": [
            {
              "Name": "Erstes Kind",
              "Kinder": [
                {
                  "Name": "Wert1",
                  "Größe": "320"
                },
                {
                  "Name": "Wert2",
                  "Größe": "320"
                },
                {
                  "Name": "Wert3",
                  "Größe": "320"
                },
                {
                  "Name": "Wert4",
                  "Größe": "320"
                }
              ]
            },
            {
              "Name": "Zweites Kind",
              "Kinder": [
                {
                  "Name": "Wert1",
                  "Größe": "320"
                },
                {
                  "Name": "Wert2",
                  "Größe": "320"
                },
                {
                  "Name": "Wert3",
                  "Größe": "320"
                },
                {
                  "Name": "Wert4",
                  "Größe": "320"
                }
              ]
            },
          ]
        }
      ]
    }

Ich knacke meinen Kopf seit einer Woche, aber ich alleine konnte es nicht herausfinden. Jemand ändert bitte die Funktion, um die Daten im hierarchischen Format zu erhalten, wie ich aktualisiert habe.

Danke im Voraus!!


6
2017-10-11 11:31


Ursprung


Antworten:


Aktualisiert, um eine rekursive Methode zu verwenden

Dies sollte funktionieren n Ebenen statt nur 2 oder 3. Sie müssen nur angeben, welche Eigenschaften welche Ebenen definieren.

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 });
    });
});

14
2017-10-11 12:10



var data = [
    { "state": "UP", "district": "Agra", "block": "IradatNagar","school":"APS","name":"A" },
    { "state": "UP", "district": "Agra", "block": "IradatNagar","school":"IPS","name":"B" },
    { "state": "UP", "district": "Agra", "block": "IradatNagar","school":"APS","name":"C" },
    { "state": "MP", "district": "Bhopal", "block": "chota_Bhopal","school":"DPS","name":"D" },
    { "state": "UP", "district": "Mathura", "block": "Farah","school":"HPS","name":"E" },
    { "state": "UP", "district": "Kanpur", "block": "Mania","school":"BPs","name":"F" },
    { "state": "UP", "district": "Agra", "block": "Arjun Nagar","school":"GPS","name":"G" }, 
    { "state": "MP", "district": "Gwalior", "block": "Surya Nagar","school":"DPS","name":"H" }
];

var newData = { name :"State", children : [] },
    levels = ["state","district","block","school"];

data.forEach(function(d){
    var depthCursor = newData.children;
    levels.forEach(function( property, depth )
    {
        var index;
        depthCursor.forEach(function(child,i)
        {
            if ( d[property] == child.name ) 
                index = i;
        });

        if ( isNaN(index) ) 
        {
            depthCursor.push({name : d[property], children : []});
            index = depthCursor.length - 1;
        }

        depthCursor = depthCursor[index].children;

        if ( depth === levels.length - 1 )
        {
            depthCursor.push({ name : d.name});
        }
    });
});

console.log(newData);

1
2017-11-09 09:23