Error parsing data org.json.JSONException: Unterminated array at character
I am new to JSON and am not aware of the constraints that need to be
followed for an Arraylist to successfully convert to a JSONArray. I am
trying to send an arraylist of multiple string elements from a servlet to
a JSON Parser. The same works when I have small elements e.g. [Resolved,
Working] but my JSON Parser class throws an error if an array element
contains spaces or special characters. I get the below error while
parsing:
08-20 16:46:13.480: E/JSON Parser(475): Error parsing data
org.json.JSONException: Unterminated array at character 17 of [Closed,
Emails not working in A-82 premises ; Requested Username: P Smith]
JSON Parser code:
public class JSONParser {
static InputStream is = null;
static JSONArray jArr = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONArray getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
//HttpPost httpPost = new HttpPost(url);
HttpGet httpPost = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
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();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jArr = new JSONArray(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jArr;
}
}
No comments:
Post a Comment