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

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

我有一个有效的 JSON 文件。我想将它作为 JSONArray 对象读入内存。

为此,我使用了以下代码(由 Apache Commons IO 2.5 提供支持):

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

但我收到错误:

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 il.ac.haifa.is.infomedia.dba.NewsSourcesData.loadNewsSourcesData(NewsSourcesData.java:39)
    at il.ac.haifa.is.infomedia.main.Main.main(Main.java:65)

经过多次检查,我发现问题的根源是文件编码。如果我在 UTF-8 中提供文件,它会抛出异常,但如果我将文件转换为 ANSI,一切正常。

我正在使用 org.json 版本 json-20160212.jar.

我的问题:
如何从 UTF-8 编码的 JSON 文件中获取 JSONArray

我认为你有一个文件编码 UTF-8 with BOM 尝试使用 Notepad++ 或类似于没有 BOM 的 UTF-8 的东西来转换文件