Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JsonWriter在Gson中写入数据,而不重写/删除以前存储的数据_Java_Json_Gson_Writing - Fatal编程技术网

Java 如何使用JsonWriter在Gson中写入数据,而不重写/删除以前存储的数据

Java 如何使用JsonWriter在Gson中写入数据,而不重写/删除以前存储的数据,java,json,gson,writing,Java,Json,Gson,Writing,因此,在我运行代码之前,这里是example.json: { "example1": 5 } {"example2":13} 当我执行此代码时: JsonWriter exampleWriter = new JsonWriter(new FileWriter(examplePath)); exampleWriter.beginObject(); exampleWriter.name("example2").value(13);

因此,在我运行代码之前,这里是example.json:

{
  "example1": 5
}
{"example2":13}
当我执行此代码时:

JsonWriter exampleWriter = new JsonWriter(new FileWriter(examplePath));
exampleWriter.beginObject();
exampleWriter.name("example2").value(13);
exampleWriter.endObject();
exampleWriter.close();
然后example.json就会发生这种情况:

{
  "example1": 5
}
{"example2":13}
我希望example.json包含example1和example2的数据,我该怎么做?

试试看 JsonWriter exampleWriter=newJSONWriter(newFileWriter(examplePath,true))

试试看
JsonWriter exampleWriter=newJSONWriter(newFileWriter(examplePath,true))

您可以将整个
JSON
负载读取为
JsonObject
并添加新属性。之后,您可以将其序列化回
JSON

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class GsonApp {

    public static void main(String[] args) throws IOException {
        Path pathToJson = Paths.get("./resource/test.json");

        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        try (BufferedReader reader = Files.newBufferedReader(pathToJson);
             BufferedWriter writer = Files.newBufferedWriter(pathToJson, StandardOpenOption.WRITE)) {
            JsonObject root = gson.fromJson(reader, JsonObject.class);
            root.addProperty("example2", 13);
            gson.toJson(root, writer);
        }
    }
}
上述代码生成:

{
  "example1": 5,
  "example2": 13
}
另见:


您可以将整个
JSON
负载读取为
JsonObject
并添加新属性。之后,您可以将其序列化回
JSON

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class GsonApp {

    public static void main(String[] args) throws IOException {
        Path pathToJson = Paths.get("./resource/test.json");

        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        try (BufferedReader reader = Files.newBufferedReader(pathToJson);
             BufferedWriter writer = Files.newBufferedWriter(pathToJson, StandardOpenOption.WRITE)) {
            JsonObject root = gson.fromJson(reader, JsonObject.class);
            root.addProperty("example2", 13);
            gson.toJson(root, writer);
        }
    }
}
上述代码生成:

{
  "example1": 5,
  "example2": 13
}
另见: