A JSONObject text must begin with '{' at 1 [character 2 line 1] with '{' error

前端 未结 12 572
失恋的感觉
失恋的感觉 2021-01-01 13:34
String JSON = \"http://www.json-generator.com/j/cglqaRcMSW?indent=4\";

JSONObject jsonObject = new JSONObject(JSON);
JSONObject getSth = jsonObject.getJSONObject(\"         


        
相关标签:
12条回答
  • 2021-01-01 14:01

    I had the same, there was an empty new line character at the beginning. That solved it:

    int i = result.indexOf("{");
    result = result.substring(i);
    JSONObject json = new JSONObject(result.trim()); 
    System.out.println(json.toString(4));  
    
    0 讨论(0)
  • 2021-01-01 14:02

    Your problem is that String JSON = "http://www.json-generator.com/j/cglqaRcMSW?indent=4"; is not JSON.
    What you want to do is open an HTTP connection to "http://www.json-generator.com/j/cglqaRcMSW?indent=4" and parse the JSON response.

    String JSON = "http://www.json-generator.com/j/cglqaRcMSW?indent=4";
    JSONObject jsonObject = new JSONObject(JSON); // <-- Problem here!
    

    Will not open a connection to the site and retrieve the content.

    0 讨论(0)
  • 2021-01-01 14:04

    I had similar issue due to a small mistake, when i was trying to convert a List to json. If a List is converted to json it will return JSONArray not JSONObject.

    0 讨论(0)
  • 2021-01-01 14:09

    in my case my arraylist trhows me that error with the JSONObject , but i fin this solution for my array of String objects

    List<String> listStrings= new ArrayList<String>();
    String json = new Gson().toJson(listStrings);
    return json;
    

    Works like charm with angular Gson version 2.8.5

    0 讨论(0)
  • 2021-01-01 14:10

    In my case the json file encoding was a problem

    I was generating JSON file in vb .net with following: My.Computer.FileSystem.WriteAllText("newComponent.json", json.Trim, False)

    And I tried all suggestions in this post but none helped.

    Eventually in Notepad++ noticed that the file created was in UTF-8-BOM

    Not sure how it picked up that encoding but after I switched the encoding to UTF-8, it resolved with no other changes.

    Hope this helps someone.

    0 讨论(0)
  • 2021-01-01 14:10

    Your JSON is perfectly valid. Try using these JSON classes to parse it. http://json.org/java/

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