String JSON = \"http://www.json-generator.com/j/cglqaRcMSW?indent=4\";
JSONObject jsonObject = new JSONObject(JSON);
JSONObject getSth = jsonObject.getJSONObject(\"
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));
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.
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.
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
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.
Your JSON is perfectly valid. Try using these JSON classes to parse it. http://json.org/java/