简体   繁体   English

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

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

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$1.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! 非常感谢!

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 FileNotFoundException as well. 您还需要处理FileNotFoundException

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. 正如@JaredRummler所建议的那样,您需要首先将文件的内容读取到String中,然后将该String传递给JSONArray构造函数。 Here's some sample code that uses Apache Commons IO to read the file: 这是一些使用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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.json.JSONException:JSONArray文本必须以&#39;[&#39;at 1 [character 2 line 1]开头 - org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1] JSON错误:解析数据org.json.JSONException时出错:JSONArray文本必须在字符1处以&#39;[&#39;开头,为什么? - JSON Error: Error parsing data org.json.JSONException: A JSONArray text must start with '[' at character 1 of why? 尝试解析 JSON 字符串时出错。 org.json.JSONException:JSONArray 文本必须以 '[' 开头 - Getting error when trying parse JSON string. org.json.JSONException: A JSONArray text must start with '[' org.json.JSONException:JSONArray [0]不是字符串 - org.json.JSONException: JSONArray[0] not a string org.json.JSONException:找不到JSONArray [20] - org.json.JSONException: JSONArray[20] not found org.json.JSONException:JSONArray [0]不是JSONObject - org.json.JSONException: JSONArray[0] is not a JSONObject org.json.JSONException: JSONArray[0] 不是 JSONObject - Java - org.json.JSONException: JSONArray[0] is not a JSONObject - Java org.json.JSONException:JSONArray [0]不是字符串异常 - org.json.JSONException: JSONArray[0] not a string Exception org.json.JSONException:JSONObject [“地址”]不是JSONArray - org.json.JSONException: JSONObject[“address”] is not a JSONArray org.json.JSONException:JSONObject文本必须在字符1处以“ {”开头 - org.json.JSONException: A JSONObject text must begin with '{' at character 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM