我正在从本地目录读取 JSON 提要,并希望在将其保存到其他文件之前对其进行修改。
结构如下:
[
{
"title":"chapter Title",
"arr":[
{
"title":"SubChapter Title",
"arr":[
[
"Sub Sub Chapter Title",
272,
1124,
10550,
11044,
-11172,
......
],
........
............
...........
.......
我需要在现有JSON
文件中添加问题标题和问题编号,例如:
[
{
"title":"chapter Title",
"arr":[
{
"title":"SubChapter Title",
"arr":[
[
"Sub Sub Chapter Title",
272, "This is a title",
1124, "This is another title",
10550, ".....",
11044, "......",
-11172, "......",
......
],
........
............
...........
.......
我已经将问题标题映射到问题 No 以添加它们。我阅读了提要字符串并找出problem No
用problem No
. problem Title
这是我的尝试代码:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import com.google.gson.stream.JsonReader;
public class JacksonStreamAPIExample {
static String PROBLEM_LIST_FILE_PATH = "F:\\problemList.txt";
static String COMPETITIVE_PROGRAMMING_BOOK_PATH = "F:\\competitive_programming_edition_3.json";
static String TARGET_PATH = "F:\\target.json";
static HashMap<Integer, String> problems = new HashMap<Integer, String>();
public static void main(String[] args) {
// Mapping problem No with problem Title
InputStream is = null;
try {
is = new FileInputStream(PROBLEM_LIST_FILE_PATH);
} catch (FileNotFoundException e1) {
}
JsonReader reader = null;
try {
reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
}
try {
reader.beginArray();
while (reader.hasNext()) {
reader.beginArray();
reader.skipValue();
problems.put(reader.nextInt(), reader.nextString());
reader.skipValue();
while (reader.hasNext())
reader.skipValue();
reader.endArray();
}
reader.endArray();
reader.close();
} catch (IOException e) {
}
// Reading and modifying
InputStream inputStream = null;
try {
inputStream = new FileInputStream(COMPETITIVE_PROGRAMMING_BOOK_PATH);
} catch (FileNotFoundException e) {
System.out.println("file not found!");
}
InputStreamReader isr = null;
isr = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader read = new BufferedReader(isr, 8);
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = read.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
}
try {
inputStream.close();
} catch (IOException e) {
}
String jsonString = sb.toString();
try {
String regex = ", (-?\\d+)";
Pattern myPattern = Pattern.compile(regex);
Matcher regexMatcher = myPattern.matcher(jsonString);
while (regexMatcher.find()) {
for (int i = 1; i <= regexMatcher.groupCount(); i++) {
System.out.println("I want to add " + problems.get(Math.abs(Integer.parseInt(regexMatcher.group(i)))) + " after " + regexMatcher.group(i) + " in jsonString and write it in target file.");
}
}
} catch (PatternSyntaxException ex) {
}
Path path = Paths.get(TARGET_PATH);
try {
Files.write(path, jsonString.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
}
}
}
一切正常。只有在问题 No 之后替换/添加问题标题不起作用。我怎样才能完成这项工作?