简体   繁体   中英

Create a file tree format in JSON based on other JSON data

I want to create a file tree format in JSON based on other JSON. The idea is to reorganize the JSON data in a tree format.

Note that the name of the file indicates where it is located in the tree.

Let's suppose that we have this first JSON:

    {
        "dir1/file3.xyz":
        [
            18KB,
            "2020-01-01 00:00:00"
        ],
        "dir1/file4.xyz":
        [
            18KB,
            "2020-01-02 00:00:00"
        ],
        "dir1/subdir1/file5.xyz":
        [
            18KB,
            "2020-01-01 00:00:00"
        ],
        "dir1/subdir1/file6.xyz":
        [
            18KB,
            "2020-01-02 00:00:00"
        ],
        "file1.xyz":
        [
            18KB,
            "2020-01-01 00:00:00"
        ],
        "file2.xyz":
        [
            18KB,
            "2020-01-02 00:00:00"
        ]
    }

I want to create a JSON based on the previous one in this format:

    [
    directories: [
        {
            name: 'dir1',
            directories: [
                {
                    name: 'subdir1',
                    directories: [],
                    files: [
                        {
                            name: 'file5.xyz',
                            size: '19KB',
                            modified: '2020-01-01 00:00:00'
                        },
                        {
                            name: 'file6.xyz',
                            size: '20KB',
                            modified: '2020-01-02 00:00:00'
                        }
                    ]
                }
            ],
            files: [
                {
                    name: 'file3.xyz',
                    size: '19KB',
                    modified: '2020-01-01 00:00:00'
                },
                {
                    name: 'file4.xyz',
                    size: '20KB',
                    modified: '2020-01-02 00:00:00'
                }
            ]
        }
    ],
    files: [
        {
            name: 'file1.xyz',
            size: '19KB',
            modified: '2020-01-01 00:00:00'
        },
        {
            name: 'file2.xyz',
            size: '20KB',
            modified: '2020-01-02 00:00:00'
        }
    ]

Thank you for your time.

We solved this challenge using the JavaScript code logic below:

function fillWithZero(intValue){
    return intValue > 9 ? intValue : '0' + intValue;
}
function list_to_tree(list) {
    var map = {}, i;
    var root = {};
    root.text = '/';
    root.value = '';
    root.disabled = true;
    root.children = [];
    map['/'] = root;
    for (i = 0; i < list.length; i += 1) {
        node = list[i];
        var current = {};
        var d = node.date;
        var datestring = fillWithZero(d.getDate()) + "-" + fillWithZero(d.getMonth()+1) + "-" + d.getFullYear() + " " + fillWithZero(d.getHours()) + ":" + fillWithZero(d.getMinutes()) + ":" + fillWithZero(d.getSeconds());
        current.text = node.name + ' - Size: ' + parseInt(node.size/1024) + 'KB' + ' Modified: ' + datestring;
        current.value = node.name;
        var parent = root;
        if (node.name.indexOf('/') > 0) {
            var paths = node.name.split('/');
            for (j = 0; j < paths.length - 1; j += 1) {
                var dirId = paths[j];
                var dir = map[dirId];
                if (dir == null) {
                    dir = {};
                    dir.text = dirId;
                    dir.value = dirId;
                    if(parent != root){
                        dir.value = parent.value + '/' + dirId;
                    }
                    dir.disabled = true;
                    dir.children = [];
                    map[dirId] = dir;                   
                    parent.children.push(dir);
                }
                parent = dir;
            }
            current.text = node.name.substr(node.name.lastIndexOf('/') + 1, node.name.length) + ' - Size: ' + parseInt(node.size/1024) + 'KB' + ' Modified: ' + datestring;
        }
        parent.children.push(current);
    }
    return root;
}
var entries = [{
        "name": "dir1/file1.xyz",
        "size": 4234,
        "date": new Date()
    }, {
        "name": "dir1/file2.xyz",
        "size": 4234,
        "date": new Date()
    }, {
        "name": "dir1/subdir1/file3.xyz",
        "size": 4234,
        "date": new Date()
    }, {
        "name": "dir1/subdir1/file4.xyz",
        "size": 4234,
        "date": new Date()
    }, {
        "name": "file3.xyz",
        "size": 4234,
        "date": new Date()
    }, {
        "name": "file4.xyz",
        "size": 4234,
        "date": new Date()
    }
];
console.log(list_to_tree(entries));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM