java org.json.JSONException:JSONArray 文本必须以“[”开头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27517649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:55:58  来源:igfitidea点击:

org.json.JSONException: A JSONArray text must start with '['

javajson

提问by user2320431

I have the following json file:

我有以下 json 文件:

[   { "1" : "b1.png"},
    { "2" : "bb1.png"},
    { "3" : "bbg1.png"}
]

and in the following line:

并在以下行中:

JSONArray array = new JSONArray(jsonFilePath);

I get the following exception:

我收到以下异常:

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:105)
    at org.json.JSONArray.<init>(JSONArray.java:144)
    at il.ac.technion.cs234311.dolphins.parse.ParseCardsTest.initialize(ParseCardsTest.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

But it is start with '[' !!!

但它以 '[' 开头!!!

Any ideas? Thanks a lot!

有任何想法吗?非常感谢!

回答by August

Create a JSONTokener:

创建一个JSONTokener

Reader in = new InputStreamReader(new FileInputStream(jsonFilePath), StandardCharsets.UTF_8);
JSONArray array = new JSONArray(new JSONTokener(in));
in.close();

You'll need to handle FileNotFoundExceptionas well.

你也需要处理FileNotFoundException

回答by dnault

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 IOto read the file:

正如@JaredRummler 建议的那样,您需要先将文件的内容读入一个字符串,然后将该字符串传递给 JSONArray 构造函数。下面是一些使用Apache Commons IO读取文件的示例代码:



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));