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

android, java, json, parsing

Solution

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.

Problem

``` String JSON = "http://www.json-generator.com/j/cglqaRcMSW?indent=4"; JSONObject jsonObject = new JSONObject(JSON); JSONObject getSth = jsonObject.getJSONObject("get"); Object level = getSth.get("2"); System.out.println(level); ``` I referred many solutions for parsing this link, still getting the same error in question. Can any give me a simple solution for parsing it.

Original source