json中的空数组问题
我编写了一个函数,它扫描给定目录中的所有文件/目录并返回一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后一个甚至不是有效的 JSON。该规范定义空数组是有效且允许的。尝试:
使用 JSONLint 验证您的 JSON 对象。
The final one is not even valid JSON. The spec defines that empty arrays are valid and allowed. Try:
Use JSONLint to validate your JSON objects.