Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONException: Value of type java.lang.String cannot be converted to JSONObject

I have a JSON file with 2 JSON-Arrays in it: One Array for routes and one Array for sights.

A route should consist of several sights where the user gets navigated to. Unfortunately I am getting the error:

JSONException: Value of type java.lang.String cannot be converted to JSONObject

Here are my variables and the code that parses the JSON-File:

private InputStream is = null; private String json = ""; private JSONObject jObj = null;  try {     BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);     StringBuilder sb = new StringBuilder();     String line = null;     while ((line = reader.readLine()) != null) {         sb.append(line + "\n");     }     is.close();     // hier habe ich das JSON-File als String     json = sb.toString();     Log.i("JSON Parser", json); } catch (Exception e) {     Log.e("Buffer Error", "Error converting result " + e.toString()); }  // try parse the string to a JSON object try {     jObj = new JSONObject(json); } catch (JSONException e) {     Log.e("JSON Parser", "Error parsing data " + e.toString()); }  // return JSON String return jObj; } 

Log.i("JSON Parser", json); shows me that at the beginning of the generated string there is a strange sign: enter image description here

but the error happens here:

try {     jObj = new JSONObject(json); } catch (JSONException e) {     Log.e("JSON Parser", "Error parsing data " + e.toString()); } 

04-22 14:01:05.043: E/JSON Parser(5868): Error parsing data org.json.JSONException: Value //STRANGE SIGN HERE // of type java.lang.String cannot be converted to JSONObject

anybody has a clue on how to get rid of these signs in order to create the JSONObject?

like image 388
RCK69 Avatar asked Apr 22 '12 12:04

RCK69


People also ask

Can we convert String to json in Java?

There are the following three libraries are used to convert String to JSON Object in Java: Using Gson Library. Using JSON-Simple Library. Jackson Library.

What is org json JSONException?

JSONPointerException. The JSONPointerException is thrown by JSONPointer if an error occurs during evaluating a pointer. Methods in org.json that return JSONException. Modifier and Type. Method and Description.

How do I use getJsonObject?

getJsonObject() MethodIt is used to get the (JsonObject)get(name). The method parses an argument name of type String whose related value is to be returned. It returns the object of the associated mapping for the parse's parameter. It returns null if the object has no mapping for the parameter.

What is json Perl?

JSON FunctionsConverts the given Perl data structure to a json string. from_json. Expects a json string and tries to parse it, returning the resulting reference. convert_blessed. Use this function with true value so that Perl can use TO_JSON method on the object's class to convert an object into JSON.


1 Answers

Reason is some un-wanted characters was added when you compose the String. The temp solution is

return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1)); 

But try to remove hidden characters on source String.

like image 167
Khai Nguyen Avatar answered Sep 20 '22 17:09

Khai Nguyen