A JSONArray text must start with '[' at 1 [character 2 line 1]

后端 未结 2 413
梦谈多话
梦谈多话 2021-01-01 06:06

I have a JSON file and i am trying to deal with but the following error is appears:

Exception in thread \"main\" org.json.JSONException: A JSONObject

相关标签:
2条回答
  • 2021-01-01 06:24

    The answer is :

    instead of JSONArray jsonarray1=new JSONArray(res.toString());

    write like this

    JSONArray jsonarray1=new JSONArray(res.asString());

    0 讨论(0)
  • 2021-01-01 06:41

    You have to read the content of the file first, because the constructor of JSONArray needs the file-content and not the file-path.

    new JSONObject(new JSONTokener(new FileInputStream(new File("path"), "UTF-8")));
    
    new JSONObject(new JSONTokener(new FileReader("path")));
    

    update You should use a filereader or specify the charset for the FileInputStream

    0 讨论(0)
提交回复
热议问题