[[techno:java spring boot]
有一个.txt文件,其中包含:
{ "foo": "one" }
{ "bar": "two" }
[
{ "foo": "one" },
{ "bar": "two" }
]
loop(contentFile as line) {
// line = { "foo": "one" }
}
您可以遍历每行,然后尝试将行的内容解析为JSONObject,然后将所有这些JSONObject收集到JSONArray中,然后将序列化的Array写入json文件中。
例如,使用json-simple或javax.json。
您可以像下面这样逐行读取Java文件:
try {
BufferedReader reader reader = new BufferedReader(new FileReader(path-to-json-file));
String line = reader.readLine(); //here line = { "foo": "one" }
while (line != null) {
//do required opertaion and read next line
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}