在JSON中的空数组问题

huangapple go评论128阅读模式
英文:

null array problem in json

问题

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

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

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"]
        }
    ]
}

(当我传递上述数据结构格式时,它可能不起作用,因为"children"为空)

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

我该如何解决这个构建Directory树的问题?

英文:

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?

答案1

得分: 1

最后一个甚至不是有效的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.

huangapple
  • 本文由 发表于 2011年7月7日 22:44:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/6612481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定