List of usage examples for org.json JSONArray toString
public String toString(int indentFactor) throws JSONException
From source file:ac.blitz.acme.video.VideoCaptureDeviceInfoAndroid.java
private static String getDeviceInfo() { try {/* w w w . j a v a 2 s . co m*/ JSONArray devices = new JSONArray(); for (int i = 0; i < Camera.getNumberOfCameras(); ++i) { CameraInfo info = new CameraInfo(); Camera.getCameraInfo(i, info); String uniqueName = deviceUniqueName(i, info); JSONObject cameraDict = new JSONObject(); devices.put(cameraDict); List<Size> supportedSizes; List<int[]> supportedFpsRanges; try { Camera camera = Camera.open(i); Parameters parameters = camera.getParameters(); supportedSizes = parameters.getSupportedPreviewSizes(); supportedFpsRanges = parameters.getSupportedPreviewFpsRange(); camera.release(); Log.d(TAG, uniqueName); } catch (RuntimeException e) { Log.e(TAG, "Failed to open " + uniqueName + ", skipping"); continue; } JSONArray sizes = new JSONArray(); for (Size supportedSize : supportedSizes) { JSONObject size = new JSONObject(); size.put("width", supportedSize.width); size.put("height", supportedSize.height); sizes.put(size); } // Android SDK deals in integral "milliframes per second" // (i.e. fps*1000, instead of floating-point frames-per-second) so we // preserve that through the Java->C++->Java round-trip. int[] mfps = supportedFpsRanges.get(supportedFpsRanges.size() - 1); cameraDict.put("name", uniqueName); cameraDict.put("front_facing", isFrontFacing(info)).put("orientation", info.orientation) .put("sizes", sizes).put("min_mfps", mfps[Parameters.PREVIEW_FPS_MIN_INDEX]) .put("max_mfps", mfps[Parameters.PREVIEW_FPS_MAX_INDEX]); } String ret = devices.toString(2); return ret; } catch (JSONException e) { throw new RuntimeException(e); } }
From source file:com.xee.demo.utils.JsonLogger.java
/** * Get a json to format//from w w w .j a v a2 s. com * @param message the json to format * @return the pretty json */ public static String getPrettyLog(String message) { String json; try { if (message.startsWith("{")) { JSONObject jsonObject = new JSONObject(message); json = jsonObject.toString(JSON_INDENT); } else if (message.startsWith("[")) { JSONArray jsonArray = new JSONArray(message); json = jsonArray.toString(JSON_INDENT); } else { json = message; } } catch (JSONException e) { json = message; } return json; }
From source file:com.endofhope.neurasthenia.bayeux.BayeuxMessage.java
@Override public String toString() { JSONObject jsonObject = null;/*ww w. j av a 2 s . co m*/ try { if (type == BayeuxMessage.HANDSHAKE_REQ) { jsonObject = convertHandshakeReq(); } else if (type == BayeuxMessage.HANDSHAKE_RESP) { jsonObject = convertHandshakeResp(); } else if (type == BayeuxMessage.CONNECT_REQ) { jsonObject = convertConnectReq(); } else if (type == BayeuxMessage.CONNECT_RESP) { jsonObject = convertConnectResp(); } else if (type == BayeuxMessage.DISCONNECT_REQ) { jsonObject = convertDisconnectReq(); } else if (type == BayeuxMessage.DISCONNECT_RESP) { jsonObject = convertDisconnectResp(); } else if (type == BayeuxMessage.SUBSCRIBE_REQ) { jsonObject = convertSubscribeReq(); } else if (type == BayeuxMessage.SUBSCRIBE_RESP) { jsonObject = convertSubscribeResp(); } else if (type == BayeuxMessage.UNSUBSCRIBE_REQ) { jsonObject = convertUnsubscribeReq(); } else if (type == BayeuxMessage.UNSUBSCRIBE_RESP) { jsonObject = convertUnsubscribeResp(); } else if (type == BayeuxMessage.PUBLISH_REQ) { jsonObject = convertPublishReq(); } else if (type == BayeuxMessage.PUBLISH_RESP) { jsonObject = convertPublishResp(); } else if (type == BayeuxMessage.DELIVER_EVENT) { jsonObject = convertDeliverEvent(); } else { throw new JSONException("Invalid BayeuxMessageType " + type); } } catch (JSONException e) { logger.log(Level.WARNING, "Invalid json", e); } JSONArray jsonArray = new JSONArray(); jsonArray.put(jsonObject); String result = null; try { result = jsonArray.toString(4); } catch (JSONException e) { logger.log(Level.WARNING, "Invalid json array", e); } return result; }
From source file:com.kevinquan.android.utils.JSONUtils.java
/** * Returns a "pretty print" string version of the JSONArray * @param array The array to pretty print * @param indentation The amount of indentation for each level * @return A pretty string /*from w ww . ja va 2 s . c o m*/ */ public static String prettyPrint(JSONArray array, int indentation) { if (array == null) { return new String(); } try { return array.toString(indentation); } catch (JSONException je) { Log.w(TAG, "Could not pretty print JSON: " + array.toString()); return new String(); } }
From source file:org.loklak.api.iot.YahiHazeServlet.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Query post = RemoteAccess.evaluate(request); // manage DoS if (post.isDoS_blackout()) { response.sendError(503, "your request frequency is too high"); return;// w w w. j a v a 2 s . c om } String yahiURL = "http://yahi-map-data.rolandturner.com/yahi-map-data.json"; JSONArray json = readJsonFromUrl(yahiURL); PrintWriter sos = response.getWriter(); sos.print(json.toString(2)); sos.println(); }
From source file:pt.webdetails.cfr.CfrContentGenerator.java
@Exposed(accessLevel = AccessLevel.PUBLIC, outputType = MimeType.JSON) public void listFilesJSON(OutputStream out) throws IOException, JSONException { String baseDir = checkRelativePathSanity(getRequestParameters().getStringParameter("dir", "")); IFile[] files = service.getRepository().listFiles(baseDir); JSONArray arr = new JSONArray(); if (files != null) { for (IFile file : files) { if (mr.isCurrentUserAllowed(FilePermissionEnum.READ, relativeFilePath(baseDir, file.getName()))) { JSONObject obj = new JSONObject(); obj.put("fileName", file.getName()); obj.put("isDirectory", file.isDirectory()); obj.put("path", baseDir); arr.put(obj);/*from w ww .j av a 2s. c o m*/ } } } writeOut(out, arr.toString(2)); }
From source file:pt.webdetails.cfr.CfrContentGenerator.java
@Exposed(accessLevel = AccessLevel.PUBLIC, outputType = MimeType.JSON) public void getPermissions(OutputStream out) throws IOException, JSONException { String path = checkRelativePathSanity(getRequestParameters().getStringParameter(pathParameterPath, null)); String id = getRequestParameters().getStringParameter(pathParameterGroupOrUserId, null); if (path != null || id != null) { JSONArray permissions = mr.getPermissions(path, id, FilePermissionMetadata.DEFAULT_PERMISSIONS); writeOut(out, permissions.toString(0)); }/* ww w . j a v a2 s . co m*/ }
From source file:com.emergent.android.weave.client.WeaveUtil.java
@SuppressWarnings("unused") private static HttpEntity toHttpEntity(JSONArray jsonArray) throws JSONException { try {//from w w w .j a va 2 s .co m StringEntity entity = new StringEntity(jsonArray.toString(0), ENTITY_CHARSET_NAME); entity.setContentType(JSON_STREAM_TYPE + HTTP.CHARSET_PARAM + ENTITY_CHARSET_NAME); return entity; } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } }
From source file:ru.jkff.antro.ProfileListener.java
public void dumpReport(Report report, String reportFilename) { try {//from www .j a v a 2 s. co m JSONArray res = new JSONArray(); for (String file : report.getUsedBuildFiles()) { AnnotatedFile f = report.getAnnotatedFile(file); JSONArray annotatedFile = new JSONArray(); for (int i = 0; i < f.getLineCount(); ++i) { String line = f.getLine(i); Stat stat = f.getStat(i); annotatedFile.put(annotateLine(line, i, stat)); } JSONObject fileObj = new JSONObject(); fileObj.put("name", file); fileObj.put("stat", annotatedFile); res.put(fileObj); } if (!dontWriteFile) { FileWriter w = new FileWriter(reportFilename); JSONArray data = new JSONArray(); data.put(res); data.put(toJSON(report.getTrace())); w.write(data.toString(2)); w.close(); } } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.jiongbull.jlog.printer.JsonPrinter.java
@Override public void printConsole(LogLevel level, String tag, String message, StackTraceElement element) { String json;//from w w w. ja v a 2 s . c o m try { if (message.startsWith("{")) { JSONObject jsonObject = new JSONObject(message); json = jsonObject.toString(JSON_INDENT); } else if (message.startsWith("[")) { JSONArray jsonArray = new JSONArray(message); json = jsonArray.toString(JSON_INDENT); } else { json = message; } } catch (JSONException e) { json = message; } PrinterUtils.printConsole(level, tag, PrinterUtils.decorateMsgForConsole(json, element)); }