String random = "{\"url\":\"api.github.com/repos/SparkTC/stocator/issues/comments/206712399\",\"html_url\":\"github.com/SparkTC/stocator/issues/22#issuecomment-206712399\"";
System.out.println(random.replaceAll("\\\"", ""));
Output: {"url":"api.github.com/repos/SparkTC/stocator/issues/comments/206712399","html_url":"github.com/SparkTC/stocator/issues/22#issuecomment-206712399"
ただし、ファイル内のコンテンツが同じで、次のコードを使用しても何も変わりません。JavaのreplaceAll()で奇妙な問題が発生しました。
File file = new File("/Users/ayacs/Desktop/example.json");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "", oldtext = "";
while ((line = reader.readLine()) != null) {
oldtext += line + "\r\n";
}
reader.close();
String newtext = oldtext.replaceAll("\\\"", "\"");
System.out.println(oldtext);
System.out.println(newtext);
FileWriter writer = new FileWriter("/Users/ayacs/Desktop/example.json");
writer.write(newtext);
writer.close();
と出力はファイルの内容と同じである:
{\"url\":\"api.github.com/repos/SparkTC/stocator/issues/comments/206712399\",\"html_url\":\"github.com/SparkTC/stocator/issues/22#issuecomment-206712399\"
私は私のファイルに\を削除するといくつかの問題を抱えていると思います。私は他のreplaceAllの問題も調べてみたが、私の場合は役に立たなかった。
しないでください。あなたはJSONパーサを使うべきです。 – SLaks
実際には文字列エスケープを見ているように思えますが、問題はありません。 – SLaks
なぜ '.replace()'ではなく '.replaceAll()'を使用しますか?ここには正規表現は必要ありません。 –