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.
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);
.....
}
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.
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
The file that I was using was saved through Powershell in UTF-8 format. I changed it to ANSI and it fixed the problem.
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));
Actually,,i found a simple answer,, Jst adding the object to String Builder instead of String worked ;)
StringBuilder jsonString= new StringBuilder.append("http://www.json-.com/j/cglqaRcMSW?=4");
JSON json= new JSON(jsonString.toString);
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.
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);
I had the same issue because of the wrong order of the code statements. Maintain the below order to resolve the issue. All get methods statements first and later httpClient methods.
HttpClient httpClient = new HttpClient();
get = new GetMethod(instanceUrl+usersEndPointUri);
get.setRequestHeader("Content-Type", "application/json");
get.setRequestHeader("Accept", "application/json");
httpClient.getParams().setParameter("http.protocol.single-cookie-header", true);
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
httpClient.executeMethod(get);
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.
Your JSON is perfectly valid. Try using these JSON classes to parse it. http://json.org/java/
Source: Stackoverflow.com