json中的空数组问题

发布于 2024-11-19 02:08:12 字数 932 浏览 4 评论 0原文

我编写了一个函数,它扫描给定目录中的所有文件/目录并返回一个 json 对象。我需要使用 jstree 在 UI 上显示这个 json 对象。我已经使用递归在 go 中编写了该函数来扫描以该特定文件夹为根的所有文件和目录。

这是我用来构造整个结构的类型

type Directory struct {
    Name string "data"
    SubDirs []Directory "children"
}

现在 jstree 接受以下格式的数据结构。

json_data: {
        data: [
            "f1",
            "f2",
            {
                data: "f3",
                children: ["f4", "f5"]
            }
        ]
 }

而不是格式:-(

json_data: {
        data: [
            {
              "data": "f1",
              "children": []
            } 
            {
              "data": "f2",
              "children": []
            } 
            {
                data: "f3",
                children: ["f4", "f5"]
            }
        ]
 }

当我传递上述数据结构格式时,它可能不起作用,因为“孩子”的东西是空的)

因此我维护的数据结构目录不足以构造目录结构。

如何解决同质构建目录树的问题?

I have written a function which scans all the files/directory in a given directory and returns a json object. I need to display this json object on the UI using jstree. I have written the function in go using recursion to scan all the files and directories rooted at that particular folder.

This is the type I am using to construct the whole structure

type Directory struct {
    Name string "data"
    SubDirs []Directory "children"
}

Now jstree accepts data structs of the following format.

json_data: {
        data: [
            "f1",
            "f2",
            {
                data: "f3",
                children: ["f4", "f5"]
            }
        ]
 }

and not of the format :-

json_data: {
        data: [
            {
              "data": "f1",
              "children": []
            } 
            {
              "data": "f2",
              "children": []
            } 
            {
                data: "f3",
                children: ["f4", "f5"]
            }
        ]
 }

(when I pass the above data structure format, it doesn't work probably since the "children" thing is null)

Thus the data structure Directory I have maintained doesn't suffice to construct the directory structure.

How do I solve this problem of constructing the Directory tree homogeneously?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晌融 2024-11-26 02:08:12

最后一个甚至不是有效的 JSON。该规范定义空数组是有效且允许的。尝试:

{
    "data": [
        {
            "data": "f1",
            "children": []
        },
        {
            "data": "f2",
            "children": []
        },
        {
            "data": "f3",
            "children": [
                "f4",
                "f5"
            ]
        }
    ]
}

使用 JSONLint 验证您的 JSON 对象。

The final one is not even valid JSON. The spec defines that empty arrays are valid and allowed. Try:

{
    "data": [
        {
            "data": "f1",
            "children": []
        },
        {
            "data": "f2",
            "children": []
        },
        {
            "data": "f3",
            "children": [
                "f4",
                "f5"
            ]
        }
    ]
}

Use JSONLint to validate your JSON objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文