로컬 디렉토리에서 JSON 피드를 읽고 다른 파일에 저장하기 전에 수정하고 싶습니다. 일치하는 정규 표현식을 올바르게 바꾸는 방법
This is the online version of the JSON
feed
구조는 같다 : 이미 문제의 제목을지도
[
{
"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, "......",
......
],
........
............
...........
.......
:
[
{
"title":"chapter Title",
"arr":[
{
"title":"SubChapter Title",
"arr":[
[
"Sub Sub Chapter Title",
272,
1124,
10550,
11044,
-11172,
......
],
........
............
...........
.......
내가 같은 기존 JSON
파일에 어떤 문제와 함께 문제의 제목을 추가 할 필요가 없습니다 그들을 추가하는 데 문제가 있습니다. 피드 문자열을 읽고 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) {
}
}
}
모두 괜찮습니다. 문제가 발생한 후 대체/추가 문제 제목 만 작동하지 않습니다. 어떻게해야합니까?
'Pattern myPattern = Pattern.compile (regex, Pattern.MULTILINE);'을 사용해 보셨습니까? – assylias
아니요.이 코드는 모두 내 코드입니다. 나는'Regex'라는 자바에 익숙하며 일부 완전한 코드 예제가 도움이 될 것입니다. 나에게 몇 가지 코드를 보여주십시오 :) –
다중 행 매개 변수를 추가하여 해당 행을 변경해보십시오. – assylias