Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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 在文件中的json数组中添加记录_Java_Json_Spring - Fatal编程技术网

Java 在文件中的json数组中添加记录

Java 在文件中的json数组中添加记录,java,json,spring,Java,Json,Spring,我有一个这样的JSON数组 [ {"submitted":"Bob","limit":0,"ID":123,"target":3}, {"submitted":"Kate","limit":500,"ID":3221,"target":2} ] try (FileWriter file = new FileWriter("C:/test/output.json", true); BufferedWriter bfile = new BufferedWrit

我有一个这样的JSON数组

[
    {"submitted":"Bob","limit":0,"ID":123,"target":3},
    {"submitted":"Kate","limit":500,"ID":3221,"target":2}
 ]
 try (FileWriter file = new FileWriter("C:/test/output.json", true);
         BufferedWriter bfile = new BufferedWriter(file);
         PrintWriter outFile = new PrintWriter(bfile))
    {
        outFile.write(obj.toJSONString()+System.lineSeparator());
        outFile.flush();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
我需要找到一种方法,在不覆盖文件或将所有内容加载到内存的情况下,如何将记录插入该文件

现在我是这样做的

[
    {"submitted":"Bob","limit":0,"ID":123,"target":3},
    {"submitted":"Kate","limit":500,"ID":3221,"target":2}
 ]
 try (FileWriter file = new FileWriter("C:/test/output.json", true);
         BufferedWriter bfile = new BufferedWriter(file);
         PrintWriter outFile = new PrintWriter(bfile))
    {
        outFile.write(obj.toJSONString()+System.lineSeparator());
        outFile.flush();
    }
    catch (IOException e) {
        e.printStackTrace();
    }

您可以使用org.json读取和写入json对象和数组,如下所示。但我不能确定在不将json文件读入内存的情况下编辑json文件是否可行

package yourPackage;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        JSONArray root = new JSONArray(new String(Files.readAllBytes(Paths.get("test.json"))));

        JSONObject obj = new JSONObject();
        obj.put("submitted","");
        obj.put("limit", 0);
        obj.put("ID", 123);
        obj.put("target", 3);

        root.put(obj);

        Files.write(Paths.get("test.json"), root.toString().getBytes());
    }
}
org.json的maven依赖项:

    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180130</version>
    </dependency>

org.json
json
20180130

您可以使用org.json读取和写入json对象和数组,如下所示。但我不能确定在不将json文件读入内存的情况下编辑json文件是否可行

package yourPackage;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        JSONArray root = new JSONArray(new String(Files.readAllBytes(Paths.get("test.json"))));

        JSONObject obj = new JSONObject();
        obj.put("submitted","");
        obj.put("limit", 0);
        obj.put("ID", 123);
        obj.put("target", 3);

        root.put(obj);

        Files.write(Paths.get("test.json"), root.toString().getBytes());
    }
}
org.json的maven依赖项:

    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180130</version>
    </dependency>

org.json
json
20180130

为什么不想将所有数据加载到内存中?正如我所见,你的数据非常少。是什么阻止了你这么做?@EmreSavcı这只是一个样本;它有更多的列和行,我想做一个临时解决方案,直到我们准备好我们的数据库为什么你不想把所有数据加载到内存?正如我所见,你的数据非常少。是什么阻止了你这么做?@EmreSavcı这只是一个样本;它有更多的列和行,我想做一个临时解决方案,直到我们准备好数据库