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:
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?
Had the same problem for few days. Found a solution at last. The PHP server returned some unseen characters which you could not see in the LOG or in System.out.
So the solution was that i tried to substring my json String one by one and when i came to substring(3) the error went away.
BTW. i used UTF-8 encoding on both sides. PHP side:
header('Content-type=application/json; charset=utf-8');
JAVA side:
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
So try the solution one by one 1,2,3,4...! Hope it helps you guys!
Reason is some un-wanted characters was added when you compose the String. The temp solution is
But try to remove hidden characters on source String.
if value of the Key is coming as String and you want to convert it to JSONObject,
First take your key.value into a String variable like
then convert into JSONArray
I think the problem may be in the charset that you are trying to use. It is probably best to use UTF-8 instead of iso-8859-1.
Also open whatever file is being used for your InputStream and make sure no special characters were accidentally inserted. Sometimes you have to specifically tell your editor to display hidden / special characters.
In my case the problem occured from
php
file. It gave unwanted characters.That is why ajson parsing
problem occured.Then I paste my
php code
inNotepad++
and selectEncode in utf-8 without BOM
fromEncoding
tab and running this code-My problem gone away.
see this http://stleary.github.io/JSON-java/org/json/JSONObject.html#JSONObject-java.lang.String-
JSONObject
Construct a JSONObject from a source JSON text string. This is the most commonly used` JSONObject constructor.
you try to use some thing like: