A JSONObject text must begin with '{' at 1 [character 2 line 1] with '{' error
Solution 1
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.
Solution 2
While the json begins with "[" and ends with "]" that means this is the Json Array, use JSONArray instead:
JSONArray jsonArray = new JSONArray(JSON);
And then you can map it with the List Test Object if you need:
ObjectMapper mapper = new ObjectMapper();
List<TestExample> listTest = mapper.readValue(String.valueOf(jsonArray), List.class);
Solution 3
I had same issue. My Json response from the server was having [, and, ]:
[{"DATE_HIRED":852344800000,"FRNG_SUB_ACCT":0,"MOVING_EXP":0,"CURRENCY_CODE":"CAD ","PIN":" ","EST_REMUN":0,"HM_DIST_CO":1,"SICK_PAY":0,"STAND_AMT":0,"BSI_GROUP":" ","LAST_DED_SEQ":36}]
http://jsonlint.com/ says valid json. you can copy and verify it.
I have fixed with below code as temporary solution:
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String result ="";
String output = null;
while ((result = br.readLine()) != null) {
output = result.replace("[", "").replace("]", "");
JSONObject jsonObject = new JSONObject(output);
JSONArray jsonArray = new JSONArray(output);
.....
}
Solution 4
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));
Solution 5
I had the same error and struggled to fix it, then answer above by Nagaraja JB helped me to fix it. In my case:
Was before: JSONObject response_json = new JSONObject(response_data);
Changed it to: JSONArray response_json = new JSONArray(response_data);
This fixed it.
Related videos on Youtube
Comments
-
Denny Mathew almost 3 years
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.
-
Scary Wombat about 10 yearsthis is valid json see jsonlint.com maybe it is your code?
-
Jitesh Upadhyay about 10 yearsThis given json lik is correct..your code which you tried and also please log cat
-
fge about 10 yearsThe bug is somewhere in your code; it looks like you are trying to parse a JSON value which is not an object using
JSONObject
. As an alternative, use a better JSON library, such as Jackson. -
ug_ about 10 yearsTry printing
str.charAt(0)
and see what the first char is. It could be a[
in that case its a json array. Or you might have a hidden char of some sort. -
OrhanC1 about 10 years@ns47731, he has posted a link to the JSON he's parsing and there is no [. str.charAt(0) is still a good debug step, though.
-
-
Assaf Gamliel almost 10 yearsI'm pretty sure this is incorrect. Can you explain please how exactly did this fix your problem? It's still not opening a connection to retrieve the JSON from the server and it's not a valid JSON, it's a URL. Moreover, new StringBuilder.append is not valid Java syntax. As you unaccepted my answer I want to know why this is better, thanks.
-
Itaypk almost 10 yearsThe code here is incorrect, why not use Assaf's answer?
-
Léo Natan almost 10 yearsHow exactly does using a string builder solve the problem? You are still passing a URL and trying to create a JSON object from it, which is incorrect.
-
Tim Autin over 9 yearsOr you may use JSONArray jsonArray = new JSONArray(output);
-
ucMedia over 3 yearsWorks like charm