我正在从本地目录中读取JSON源,并希望在将其保存到其他文件之前对其进行修改。
This is the online version of the JSON
feed
结构如下:
[
{
"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, "......",
......
],
........
............
...........
.......
我已经将问题标题映射到问题号,以便添加它们。我阅读了Feed字符串并找到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之后只有替换/添加问题标题不起作用。我怎么能这样做?
答案 0 :(得分:0)
@Dave Newton是对的。您应该通过创建JSON
对象JSONWriter
来执行此操作。使用JSON
实例解析JsonReader
并按JsonWriter
顺序添加它们。另外,从已完成的映射中添加problemTitle
。
幸运的是,我代表你完成了你的任务。希望稍作修改。
package demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
public class JacksonStreamAPIExample {
static String PROBLEM_LIST_FILE_PATH = "F:\\kaidul.txt";
static String COMPETITIVE_PROGRAMMING_BOOK_PATH = "F:\\competitive_programming_edition_3.json";
static String TARGET_PATH = "F:\\test.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 e) {
}
try {
JsonReader reader = new JsonReader(new InputStreamReader(is, StandardCharsets.UTF_8));
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
JsonWriter writer = null;
InputStream inputStream = null;
try {
inputStream = new FileInputStream(COMPETITIVE_PROGRAMMING_BOOK_PATH);
JsonReader reader = new JsonReader(new InputStreamReader(
inputStream));
writer = new JsonWriter(new FileWriter(TARGET_PATH));
reader.beginArray(); // array #1
writer.beginArray();
while (reader.hasNext()) {
reader.beginObject(); // object #2
writer.beginObject();
while (reader.hasNext()) {
reader.skipValue();
writer.name("title").value(reader.nextString());
reader.skipValue();
writer.name("arr");
reader.beginArray(); // array #3
writer.beginArray();
while (reader.hasNext()) {
reader.beginObject(); // object #4
writer.beginObject();
while (reader.hasNext()) {
reader.skipValue();
writer.name("title").value(reader.nextString());
reader.skipValue();
writer.name("arr");
reader.beginArray(); // array #5
writer.beginArray();
while (reader.hasNext()) {
reader.beginArray(); // array #6
writer.beginArray();
writer.value(reader.nextString());
while (reader.hasNext()) {
int problemNo = reader.nextInt();
writer.value(problemNo);
writer.value(problems.get(Math.abs(problemNo))); // additional problemTitle
}
reader.endArray(); // array #6
writer.endArray();
}
reader.endArray(); // array #5
writer.endArray();
}
reader.endObject(); // object #4
writer.endObject();
}
reader.endArray(); // array #3
writer.endArray();
}
reader.endObject(); // object #2
writer.endObject();
}
reader.endArray(); // array #1
writer.endArray();
reader.close();
writer.close();
} catch (IOException e) {
// nothing
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// nothing
}
}
}
}
}