如何使用jackson创建一个json数组,如下面的示例所示.

I tried using ObjectMapper, but this does not seem correct.

      try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
            for (Path file : ds) {
                System.out.println("name:"+file.getFileName()+
                        "\n"+
                        "mime:"+Files.probeContentType(file)+
                "\n"+
                "locked:"+!Files.isWritable(file));
            }
        } catch (IOException e) {
            System.err.println(e);
        }

Eventually I will be making a json that has the below values.

 * - (int)    size    file size in b. required
 * - (int)    ts      file modification time in unix time. required
 * - (string) mime    mimetype. required for folders, others - optionally
 * - (bool)   read    read permissions. required
 * - (bool)   write   write permissions. required
 * - (bool)   locked  is object locked. optionally
 * - (bool)   hidden  is object hidden. optionally
 * - (string) alias   for symlinks - link target path relative to root path. optionally
 * - (string) target  for symlinks - link target path. optionally

下面是我收到的一个json示例.

"files": [
    {
        "mime": "directory",
        "ts": 1334071677,
        "read": 1,
        "write": 0,
        "size": 0,
        "hash": "l1_Lw",
        "volumeid": "l1_",
        "name": "Demo",
        "locked": 1,
        "dirs": 1
    },
    {
        "mime": "directory",
        "ts": 1334071677,
        "read": 1,
        "write": 0,
        "size": 0,
        "hash": "l1_Lw",
        "volumeid": "l1_",
        "name": "Demo",
        "locked": 1,
        "dirs": 1
    },
    {
        "mime": "directory",
        "ts": 1340114567,
        "read": 0,
        "write": 0,
        "size": 0,
        "hash": "l1_QmFja3Vw",
        "name": "Backup",
        "phash": "l1_Lw",
        "locked": 1
    },
    {
        "mime": "directory",
        "ts": 1310252178,
        "read": 1,
        "write": 0,
        "size": 0,
        "hash": "l1_SW1hZ2Vz",
        "name": "Images",
        "phash": "l1_Lw",
        "locked": 1
    },
    {
        "mime": "application\/x-genesis-rom",
        "ts": 1310347586,
        "read": 1,
        "write": 0,
        "size": 3683,
        "hash": "l1_UkVBRE1FLm1k",
        "name": "README.md",
        "phash": "l1_Lw",
        "locked": 1
    }
]

EDIT 1

        Map<String, Object> filesMap = new HashMap<>();
        List<Object> files = new ArrayList<Object>();
        System.out.println("\nNo filter applied:");
        try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
            for (Path file : ds) {
                Map<String, Object> fileInfo = new HashMap<>();
                fileInfo.put("name", file.getFileName().toString());
//                Prints Files in Director
//                Files.getAttribute(file,"size");
                System.out.println("name:" + file.getFileName().toString() +
                        "\n" +
                        "mime:" + Files.probeContentType(file) +
                        "\n" +
                        "locked:" + !Files.isWritable(file));
                ObjectMapper mapper = new ObjectMapper();
                String json = mapper.writeValueAsString(fileInfo);
                files.add(json);
            }
        } 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,但我不明白为什么在{}之前和之后加上额外的引号.

{"files":["{\"name\":\"32C92124-EFCF-42C1-AFD2-8B741AE6854B.jpg\"}","{\"name\":\"58D5B83F-4065-4D6E-92BE-8181D99CB6CB.jpg\"}","{\"name\":\"7B1464A0-FBA1-429E-8A39-3DE5B539FBF8.jpg\"}","{\"name\":\"888159CF-45BE-475F-8C6A-64B3E1D97278.jpg\"}"]}

最终答案

    Map<String, Object> filesMap = new HashMap<>();
    List<Object> files = new ArrayList<Object>();
    System.out.println("\nNo filter applied:");
    try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
        for (Path file : ds) {
            Map<String, Object> fileInfo = new HashMap<>();
            fileInfo.put("name", file.getFileName().toString());
            System.out.println("name:" + file.getFileName().toString() +
                    "\n" +
                    "mime:" + Files.probeContentType(file) +
                    "\n" +
                    "locked:" + !Files.isWritable(file));
            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"; 
    }

推荐答案

You need a JsonNodeFactory:

final JsonNodeFactory factory = JsonNodeFactory.instance;

这个类有创建ArrayNodes、ObjectNodes、IntNodes、DecimalNodes、TextNodes等等的方法.ArrayNodes和ObjectNodes具有方便的变异方法,可以直接添加大多数JSON原语(非容器)值,而无需经过工厂(在内部,它们引用了这个工厂,这就是为什么).

至于AN ObjectMapper,请注意,它既是串行器(ObjectWriter)又是解串器(ObjectReader).

Json相关问答推荐

Google Page to JSON应用程序脚本出现CORS错误

使用更高级别架构中的字段值在$def内实现约束

创建Json嵌套文件 struct

从Postgres表中的JSON中提取值

使用 JSON 和相对日期设置日历视图中 SharePoint 列表项的背景 colored颜色 格式

使用 jq 重新格式化 JSON 输出

如何使用jolt规范将一个对象添加到另一个对象中并删除该对象

使用 jq 将非统一的 json 输出转换为汇总表

数据清理设计不良的 JSON 数据 - 需要有关最佳策略的建议

如何在 Flutter 中遍历嵌套的动态 JSON 文件

使用 jq 获取特定键的所有父键

MarkLogic REST 资源 API - 仅使用一个 POST 请求修补多个文档

如何为包含一些固定值并可能具有其他附加值的数组字符串创建数组 json 架构

Powershell JSON 操作

使用 Spring 和 JsonTypeInfo 注释将 JSON 反序列化为多态对象模型

将json字符反序列化为枚举

jQuery AJAX 和 JSON 格式

ASP.NET MVC 读取原始 JSON 发布数据

java.lang.BootstrapMethodError:调用站点#4 bootstrap 方法的异常,初始化 retrofit 时

在android中使用GSON解析带有动态key和value的JSON