-2

I'm trying to decode JSON file in JSON format using UTF-8,

But it's printing no value

  JSONParser jsonParser = new JSONParser();
  FileReader file = new FileReader("C:/Users/aab6cob/Desktop/jsonFile/170902_K0_RUCd_ML_F4974.txt.insights-json");
  BufferedReader filBufferedReader = new BufferedReader(file);

  String st;
  while((st=filBufferedReader.readLine())!=null){
    byte[] tempByte = st.getBytes("UTF-8"); 
    String tempString = new String(tempByte);
    System.out.println(tempString);
  }
INDRAJITH EKANAYAKE
  • 3,894
  • 11
  • 41
  • 63

2 Answers2

1

First of all, don't carry away with .json file format it is nothing but a simple text file. So you just read the file and write it in any text file using any fileStream etc.. It is quite similar to read and write a plan text file.

But if you want to load the JSON from a file into a JSON object, I advise you to read json-simple-read-write-json-examples

Good luck.

Himadri Mandal
  • 325
  • 2
  • 11
0

If you want to read a file with a specific encoding you cannot use FileReader. FileReader uses default encoding which depends on system settings. You can to use InputStreamReader's InputStreamReader(InputStream in, String charsetName) constructor for the same. Also the two lines from your code

byte[] tempByte = st.getBytes("UTF-8"); 
String tempString = new String(tempByte);

should be removed as you are just taking a string, converting if into bytes and then again in string which makes no sense. At the end JSON file is just a text file. Read more about java's I/O Streams and Character Encodings.

Harshank Bansal
  • 2,798
  • 2
  • 7
  • 22