0

I want to read a JSON file and get its content in the String variable. But, I'm getting escape characters like "\n" in it. How to read a JSON without these characters?

Input: any formatted JSON file

What I've tried:

  1. Reading file content using Files.readAllBytes`. But, this gives escape characters.enter image description here
String content = new String(Files.readAllBytes(<file_path>));
  1. Using JsonReader, it's giving error "JsonReader at line 1 column 1 path $"
Gson gson = new Gson();
JsonReader reader = new JsonReader(Files.newBufferedReader(<file_path>));
String content = gson.fromJson(reader, String.class);

Solution

String content = new String(Files.readAllBytes(<file_path>));
JSONObject obj = new JSONObject(content);
String formattedString = obj.toString();
kulsin
  • 398
  • 4
  • 18
  • 1
    look at https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java, i think you can found your answer here – CLAIN Cyril Sep 25 '20 at 14:41
  • Converting the original String to JSONObject and than again converting back to String gives a clean String. Marking the question as duplicate. – kulsin Sep 25 '20 at 14:45

0 Answers0