try { JSONArray jObj = new JSONArray(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); }
public class Main { public static int PRETTY_PRINT_INDENT_FACTOR = 4; public static String TEST_XML_STRING = "<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>"; public static void main(String[] args) { try { JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING); String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR); System.out.println(jsonPrettyPrintString); } catch (JSONException je) { System.out.println(je.toString()); } } }
protected void onPostExecute(Void v) { //parse JSON data try { JSONArray jArray = new JSONArray(result); for(i=0; i < jArray.length(); i++) { JSONObject jObject = jArray.getJSONObject(i); String name = jObject.getString("name"); String tab1_text = jObject.getString("tab1_text"); int active = jObject.getInt("active"); } // End Loop this.progressDialog.dismiss(); } catch (JSONException e) { Log.e("JSONException", "Error: " + e.toString()); } // catch (JSONException e) } // protected void onPostExecute(Void v) } //class MyAsyncTask extends AsyncTask<String, String, Void>
/** * Helper method to read an HTTP response. * @param is The InputStream. * @return An object representing the HTTP response. */ protected JSONObject readInputStream(InputStream is) throws IOException { StringBuilder stringBuilder = new StringBuilder(); BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8")); String line; while ((line = in.readLine()) != null) { stringBuilder.append(line); } JSONObject jsonObject; try { jsonObject = new JSONObject(stringBuilder.toString()); } catch (JSONException error) { throw new IOException(error.toString()); } return jsonObject; } }
@Override public boolean handleRequest(SocketLike socket, LightHttpRequest request, LightHttpResponse response) { String path = request.uri.getPath(); try { if (PATH_VERSION.equals(path)) { handleVersion(response); } else if (PATH_PAGE_LIST.equals(path)) { handlePageList(response); } else if (PATH_ACTIVATE.equals(path)) { handleActivate(response); } else { response.code = HttpStatus.HTTP_NOT_IMPLEMENTED; response.reasonPhrase = "Not implemented"; response.body = LightHttpBody.create("No support for " + path + "\n", "text/plain"); } } catch (JSONException e) { response.code = HttpStatus.HTTP_INTERNAL_SERVER_ERROR; response.reasonPhrase = "Internal server error"; response.body = LightHttpBody.create(e.toString() + "\n", "text/plain"); } return true; }
Toast.makeText(context, "Could not load the extra-keys property from the config: " + e.toString(), Toast.LENGTH_LONG).show(); Log.e("termux", "Error loading props", e); mExtraKeys = new String[0][];
@Override protected void describeMismatchSafely(final String item, final Description mismatchDescription) { try { Object itemAsPrettyJson = JSONParser.parseJSON(item); mismatchDescription.appendText("was ").appendValue(itemAsPrettyJson); } catch (final JSONException e) { throw new RuntimeException(String.format("Error parsing expected JSON string %s. Got Exception %s", item, e.toString())); } } }
@Override public void describeTo(final Description description) { try { Object expectedJsonAsPrettyJson = JSONParser.parseJSON(expectedJson); description.appendValue(expectedJsonAsPrettyJson); } catch (final JSONException e) { throw new RuntimeException(String.format("Error parsing expected JSON string %s. Got Exception %s", expectedJson, e.toString())); } }
@ChromeDevtoolsMethod public JsonRpcResult getResponseBody(JsonRpcPeer peer, JSONObject params) throws JsonRpcException { try { String requestId = params.getString("requestId"); return readResponseBody(requestId); } catch (IOException e) { throw new JsonRpcException(new JsonRpcError(JsonRpcError.ErrorCode.INTERNAL_ERROR, e.toString(), null /* data */)); } catch (JSONException e) { throw new JsonRpcException(new JsonRpcError(JsonRpcError.ErrorCode.INTERNAL_ERROR, e.toString(), null /* data */)); } }
private void logEvent( String eventName, Double valueToSum, Bundle parameters, boolean isImplicitlyLogged, @Nullable final UUID currentSessionId) { try { AppEvent event = new AppEvent( this.contextName, eventName, valueToSum, parameters, isImplicitlyLogged, currentSessionId); logEvent(event, accessTokenAppId); } catch (JSONException jsonException) { // If any of the above failed, just consider this an illegal event. Logger.log(LoggingBehavior.APP_EVENTS, "AppEvents", "JSON encoding for app event failed: '%s'", jsonException.toString()); } catch (FacebookException e) { // If any of the above failed, just consider this an illegal event. Logger.log(LoggingBehavior.APP_EVENTS, "AppEvents", "Invalid app event: %s", e.toString()); } }
public JSONObject dispatch(JsonRpcPeer peer, String methodName, @Nullable JSONObject params) throws JsonRpcException { MethodDispatchHelper dispatchHelper = findMethodDispatcher(methodName); if (dispatchHelper == null) { throw new JsonRpcException(new JsonRpcError(JsonRpcError.ErrorCode.METHOD_NOT_FOUND, "Not implemented: " + methodName, null /* data */)); } try { return dispatchHelper.invoke(peer, params); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); ExceptionUtil.propagateIfInstanceOf(cause, JsonRpcException.class); throw ExceptionUtil.propagate(cause); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (JSONException e) { throw new JsonRpcException(new JsonRpcError(JsonRpcError.ErrorCode.INTERNAL_ERROR, e.toString(), null /* data */)); } }
fail(errorMessage + e.toString()); } catch (JSONException e) { fail(errorMessage + e.toString()); } finally { if (stream != null) {
JSONObject featureCollection = new JSONObject(); try { featureCollection.put("type", "featureCollection"); JSONArray featureList = new JSONArray(); // iterate through your list for (ListElement obj : list) { // {"geometry": {"type": "Point", "coordinates": [-94.149, 36.33]} JSONObject point = new JSONObject(); point.put("type", "Point"); // construct a JSONArray from a string; can also use an array or list JSONArray coord = new JSONArray("["+obj.getLon()+","+obj.getLat()+"]"); point.put("coordinates", coord); JSONObject feature = new JSONObject(); feature.put("geometry", point); featureList.put(feature); featureCollection.put("features", featureList); } } catch (JSONException e) { Log.error("can't save json object: "+e.toString()); } // output the result System.out.println("featureCollection="+featureCollection.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()); }
//Parse string to JSON object try { jarray = new JSONObject(builder.toString()); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } //Return json to array return jarray;
try { jObj = new JSONArray(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); }
try { JSONArray ja = new JSONArray(buffer.toString()); JSONArray innerJsonArray = ja.getJsonArray(0); JSONObject object = innerJsonArray.getJSONObject(0); String title = object.getString("title"); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); }
public static String[] constructIds(Response res) throws WeiboException { try { JSONArray list = res.asJSONObject().getJSONArray("ids"); String temp = list.toString().substring(1, list.toString().length() - 1); String[] ids = temp.split(","); return ids; } catch (JSONException jsone) { throw new WeiboException(jsone.getMessage() + ":" + jsone.toString(), jsone); } }
private void resetNotificationsCounter() { String data = Settings.System.getString(mContext.getContentResolver(), "CustomWatchfaceData"); if (data == null || data.equals("")) Settings.System.putString(mContext.getContentResolver(), "CustomWatchfaceData", "{}"); try { JSONObject json_data = new JSONObject(data); json_data.put("notifications", 0); Settings.System.putString(mContext.getContentResolver(), "CustomWatchfaceData", json_data.toString()); } catch (JSONException e) { Log.e(Constants.TAG, "AmazModLauncher refreshMessages JSONException: " + e.toString()); } }
public void writeCrashReport(Context context) { File file = new File(context.getFilesDir(), crashIdentifier + ".stacktrace"); try { writeCrashReport(file); } catch (JSONException e) { HockeyLog.error("Could not write crash report with error " + e.toString()); } }