如何使用Jackson将对象附加到现有JSON文件 [英] How to append Object to existing JSON file with Jackson

查看:110
本文介绍了如何使用Jackson将对象附加到现有JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用杰克逊将对象附加到现有的JSON文件中?

How do i append an object to an existing JSON file with Jackson?

File file = new File("test.json");
if (!file.exists()) {
    file.createNewFile();
}

ObjectMapper mapper = new ObjectMapper();

ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
mapper.writeValue(file, wtf);

推荐答案

在您的问题中,附加"可能具有多种含义/解决方案,具体取决于您放置该单词的上下文.例如:

In your question, "append" can have multiple meanings/solutions depending in which context you place that word. For example:

  1. 对文件的简单追加,忽略了现有的JSON结构.
  2. 附加到文件中的现有JSON数组.
  1. A plain append to a file, ignoring existing JSON structure.
  2. Append to an existing JSON array in a file.
  1. 附加到文件中的现有JSON数组,而JSON数组尚未关闭.
  2. 在JSON数组已关闭的情况下,附加到文件中的现有JSON数组.

示例1的解决方案:

// File output: {"name":"Foo","age":20} {"name":"Bar","age":30} {"name":"Baz","age":40}
public static void plainAppendExample() {
    File file = new File("u:\\test.json");
    ObjectMapper mapper = new ObjectMapper();
    try {
        JsonGenerator g = mapper.getFactory().createGenerator(new FileOutputStream(file));

        mapper.writeValue(g, new Person("Foo", 20));
        mapper.writeValue(g, new Person("Bar", 30));
        mapper.writeValue(g, new Person("Baz", 40));
        g.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

每个writeValue()都会向文件追加一个普通的JSON对象,但会忽略以前的JSON结构.

Each writeValue() appends to the file a plain JSON object, but ignores the previous JSON structure.

示例#2.1的解决方案:

// File output: [ {"name" : "Foo", "age" : 20}, {"name" : "Bar", "age" : 30}, {"name" : "Baz", "age" : 40} ]
public static void jsonArrayAppendExample2() {
    try {
        File file = new File("u:\\test.json");
        FileWriter fileWriter = new FileWriter(file, true);

        ObjectMapper mapper = new ObjectMapper();

        SequenceWriter seqWriter = mapper.writer().writeValuesAsArray(fileWriter);
        seqWriter.write(new Person("Foo", 20));
        seqWriter.write(new Person("Bar", 30));
        seqWriter.write(new Person("Baz", 40));
        seqWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

每个write()追加到文件中现有的JSON数组的位置,直到调用close().

Where each write() appends to an existing JSON array in the file, until close() is called.

示例#2.2的解决方案: 我还没有找到解决方案.在这种情况下,解决方案应通过替换数组结束字符]来修改文件,然后执行追加.

Solution of example #2.2: I haven't found a solution for this. In this case, the solution should modify the file by replacing the array closing character ] and then perform the appends.

或者,如果性能和内存不是问题,则可以将JSON文件重新读取为Java对象,然后添加新的JSON对象,然后再次写入文件.

Or, if performance and memory is not a problem, you could re-read the JSON file to a Java object, then add a new JSON object, and then write to the file again.

注意: 请注意,我不是杰克逊专家,所以我不知道我的解决方案是否是最佳解决方案.

Note: Note that I'm not a Jackson expert, so I don't know if my solution is the best solution.

这篇关于如何使用Jackson将对象附加到现有JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆