Parsing String with JSONTokener to JSONObject OR JSONArray

bagsddb

New Member
I'm using the following code to handle REST responses from my server:\[code\]if (response.getEntity() != null) { BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); StringBuilder builder = new StringBuilder(); for (String line = null; (line = reader.readLine()) != null;) { builder.append(line).append("\n"); } JSONTokener tokener = new JSONTokener(builder.toString()); try { rr.jsonObject = new JSONObject(tokener); } catch (JSONException e) { e.printStackTrace(); Log.d("parsing", "creating json array"); try { rr.jsonArray = new JSONArray(tokener); } catch (JSONException e1) { e1.printStackTrace(); } } }\[/code\]If the response is an JSONObject, it works perfectly but if the server returns a JSONArray, the second try block also throws although it's correct json.\[quote\] 03-30 14:09:15.069: W/System.err(6713): org.json.JSONException: End of input at character 314 of [{"__className":"stdClass","char3code":"DEU","fips_name":"Germany","alternate_names":"Germany, Deutschland, Allemagne, Alemania"},{"__className":"stdClass","char3code":"USA","fips_name":"United States","alternate_names":"United States of America, Vereinigte Staaten von Amerika, \u00c9tats-Unis, Estados Unidos"}]\[/quote\]
 
Back
Top