且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

org.json.JSONException:JSONArray文本必须以"["开头

更新时间:2023-11-26 18:38:46

按照@JaredRummler的建议,您需要先将文件的内容读取到String中,然后将该String传递给JSONArray构造函数.这是一些使用 Apache Commons IO 来读取文件的示例代码:

As @JaredRummler suggested, you need to read the contents of the file into a String first, and then pass that String to the JSONArray constructor. Here's some sample code that uses Apache Commons IO to read the file:

import org.apache.commons.io.IOUtils;

...

public static String readJsonFile(String filename) throws IOException {
    try (InputStream is = new FileInputStream(filename)) {
        return IOUtils.toString(is, StandardCharsets.UTF_8);
    }    
}

....

JSONArray array = new JSONArray(readJsonFile(jsonFilePath));