1

我正在使用以下函数创建 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": [

        ]
    }
}
4

1 回答 1

2

我不知道您是如何添加该 JSON 的,但这显然不是合法的 JSON。

由于您使用的是 Jackson,这很容易:

((ObjectNode) origNode).putAll(returnedNode);

真的应该使用 aJsonNodeFactory来生成您的 JSON 数据,不要手动操作!

于 2013-06-08T22:52:45.793 回答