我用以下函数创建json。然而,json在它之前和之后都有一个额外的支撑。当我将其与其他json组合时,这会导致问题。当我使用json lint验证json时,我收到以下错误。
如何删除额外的大括号?
Parse error on line 36:
... } }, { "files": [
---------------------^
Expecting 'STRING'
这是功能。
Map<String, Object> filesMap = new HashMap<>();
List<Object> files = new ArrayList<>();
// System.out.println("\nNo filter applied:");
try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
for (Path file : ds) {
Map<String, Object> fileInfo = new HashMap<>();
if (Files.isDirectory(file)) {
fileInfo.put("mime", "directory");
}
// else {
// fileInfo.put("mime", Files.probeContentType(file));
// }
fileInfo.put("ts", Files.getLastModifiedTime(file).toMillis());
int readable = (Files.isReadable(file)) ? 1 : 0;
fileInfo.put("read", readable);
int writable = (Files.isWritable(file)) ? 1 : 0;
fileInfo.put("write", writable);
fileInfo.put("size", Files.size(file));
fileInfo.put("name", file.getFileName().toString());
int hidden = (Files.isHidden(file)) ? 1 : 0;
fileInfo.put("hidden", hidden);
files.add(fileInfo);
}
} catch (IOException e) {
System.err.println(e);
}
// files.toArray();
filesMap.put("files", files);
ObjectMapper mapper = new ObjectMapper();
String jsonString;
try {
jsonString = mapper.writeValueAsString(filesMap);
} catch (IOException e) {
jsonString = "fail"; //To change body of catch statement use File | Settings | File Templates.
}
这是返回的JSON并添加到剩余的json中。
{
"files": [
{
"ts": 1370195824000,
"hidden": 1,
"name": ".DS_Store",
"write": 1,
"read": 1,
"size": 6148
},
{
"ts": 1370219091000,
"hidden": 0,
"name": "32 weeks.jpg",
"write": 1,
"read": 1,
"size": 1963258
},
{
"ts": 1370195825000,
"hidden": 0,
"name": "_MG_2975.jpg",
"write": 1,
"read": 1,
"size": 345673
},
{
"ts": 1370195825000,
"hidden": 0,
"name": "_MG_2976.jpg",
"write": 1,
"read": 1,
"size": 13513255
},
{
"ts": 1370195825000,
"hidden": 0,
"name": "_MG_2982.jpg",
"write": 1,
"read": 1,
"size": 370187
},
{
"ts": 1370723352000,
"hidden": 0,
"name": "DIrectory",
"write": 1,
"read": 1,
"mime": "directory",
"size": 68
},
{
"ts": 1370195825000,
"hidden": 0,
"name": "IMG_0963.jpg",
"write": 1,
"read": 1,
"size": 3691532
}
]
}
这是最终的JSON
{
"cwd": {
"mime": "directory",
"ts": 1334071677,
"read": 1,
"write": 0,
"size": 0,
"hash": "l1_Lw",
"volumeid": "l1_",
"name": "Demo",
"locked": 1,
"dirs": 1
},
"options": {
"path": "Demo",
"url": "http://elfinder.org/files/demo/",
"tmbUrl": "http://elfinder.org/files/demo/.tmb/",
"disabled": [
"extract"
],
"separator": "/",
"copyOverwrite": 1,
"archivers": {
"create": [
"application/x-tar",
"application/x-gzip",
"application/x-bzip2",
"application/zip",
"application/x-rar",
"application/x-7z-compressed"
],
"extract": [
]
}
},
{
"files": [
{
"ts": 1370195824000,
"hidden": 1,
"name": ".DS_Store",
"write": 1,
"read": 1,
"size": 6148
},
{
"ts": 1370219091000,
"hidden": 0,
"name": "32 weeks.jpg",
"write": 1,
"read": 1,
"size": 1963258
},
{
"ts": 1370195825000,
"hidden": 0,
"name": "_MG_2975.jpg",
"write": 1,
"read": 1,
"size": 345673
},
{
"ts": 1370195825000,
"hidden": 0,
"name": "_MG_2976.jpg",
"write": 1,
"read": 1,
"size": 13513255
},
{
"ts": 1370195825000,
"hidden": 0,
"name": "_MG_2982.jpg",
"write": 1,
"read": 1,
"size": 370187
},
{
"ts": 1370723352000,
"hidden": 0,
"name": "DIrectory",
"write": 1,
"read": 1,
"mime": "directory",
"size": 68
},
{
"ts": 1370195825000,
"hidden": 0,
"name": "IMG_0963.jpg",
"write": 1,
"read": 1,
"size": 3691532
}
]
},
"api": "2.0",
"uplMaxSize": "16M",
"netDrivers": [
],
"debug": {
"connector": "php",
"phpver": "5.3.21-1~dotdeb.0",
"time": 0.14000010490417,
"memory": "1324Kb / 1198Kb / 128M",
"upload": "",
"volumes": [
{
"id": "l1_",
"name": "localfilesystem",
"mimeDetect": "internal",
"imgLib": "imagick"
},
{
"id": "l2_",
"name": "localfilesystem",
"mimeDetect": "internal",
"imgLib": "gd"
}
],
"mountErrors": [
]
}
}
答案 0 :(得分:2)
我不知道你是如何添加JSON的,但这显然不是合法的JSON。
由于您使用的是Jackson,因此非常简单:
((ObjectNode) origNode).putAll(returnedNode);
你应该真的使用JsonNodeFactory
来生成你的JSON数据,不要手工完成!