javajsonencodingutf-8apache-commons-io

JSON file processing error: A JSONArray text must start with '[' at 1 [character 2 line 1] when use file in UTF-8 encoding


I have a valid JSON-file. I want to read it into a memory as JSONArray object.

In order to do that I use the following code (powered by Apache Commons IO 2.5):

String jsonTxt = FileUtils.readFileToString(new File(file.json), "UTF-8");
JSONArray itemsArr = new JSONArray(jsonTxt);

But I get the error:

Exception in thread "main" org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
    at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
    at org.json.JSONArray.<init>(JSONArray.java:106)
    at org.json.JSONArray.<init>(JSONArray.java:145)
    at myOrg.infomedia.dba.NewsSourcesData.loadNewsSourcesData(NewsSourcesData.java:39)
    at myOrg.infomedia.main.Main.main(Main.java:65)

After multiple checks, I found out that the root of the problem is the file encoding. If I provide a file in UTF-8, it throws exception, but if I convert the file to ANSI everything is working correctly.

I'm using org.json version json-20160212.jar.

How can I get JSONArray from my JSON-file in UTF-8 encoding?


Solution

  • I think you have a file encode in UTF-8 with BOM try to convert file using Notepad++ or something similiar to UTF-8 without BOM