List of usage examples for org.json.simple JSONArray add
public boolean add(E e)
From source file:cpd4414.assign2.OrderQueue.java
String generateReport() { String output = ""; if (!(orderQueue.isEmpty() && ListOfOrder.isEmpty())) { JSONObject obj = new JSONObject(); JSONArray orders = new JSONArray(); for (Order o : ListOfOrder) { orders.add(o.tojsonconvert()); }/* w w w .j a v a 2s . com*/ for (Order o : orderQueue) { orders.add(o.tojsonconvert()); } obj.put("orders", orders); output = obj.toJSONString(); } return output; }
From source file:com.telefonica.iot.tidoop.apiext.hadoop.ckan.CKANRecordReaderTest.java
/** * Sets up tests by creating a unique instance of the tested class, and by defining the behaviour of the mocked * classes.// w ww .ja va2 s. c o m * * @throws Exception */ @Before public void setUp() throws Exception { // set up the instance of the tested class recordReader = new CKANRecordReader(backend, inputSplit, taskAttemptContext); // set up other instances JSONArray records = new JSONArray(); records.add((new JSONObject())); // set up the behaviour of the mocked classes when(backend.getRecords(resId, firstRecordIndex, length)).thenReturn(records); when(inputSplit.getFirstRecordIndex()).thenReturn(firstRecordIndex); when(inputSplit.getLength()).thenReturn(length); when(inputSplit.getResId()).thenReturn(resId); }
From source file:com.facebook.tsdb.tsdash.server.DataEndpoint.java
@Override @SuppressWarnings("unchecked") public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/plain"); PrintWriter out = response.getWriter(); try {//from w ww. ja v a2 s . c o m long ts = System.currentTimeMillis(); // decode parameters String jsonParams = request.getParameter("params"); if (jsonParams == null) { throw new Exception("Parameters not specified"); } JSONObject jsonParamsObj = (JSONObject) JSONValue.parse(jsonParams); long tsFrom = (Long) jsonParamsObj.get("tsFrom"); long tsTo = (Long) jsonParamsObj.get("tsTo"); JSONArray metricsArray = (JSONArray) jsonParamsObj.get("metrics"); if (metricsArray.size() == 0) { throw new Exception("No metrics to fetch"); } MetricQuery[] metricQueries = new MetricQuery[metricsArray.size()]; for (int i = 0; i < metricsArray.size(); i++) { metricQueries[i] = MetricQuery.fromJSONObject((JSONObject) metricsArray.get(i)); } TsdbDataProvider dataProvider = TsdbDataProviderFactory.get(); Metric[] metrics = new Metric[metricQueries.length]; for (int i = 0; i < metrics.length; i++) { MetricQuery q = metricQueries[i]; metrics[i] = dataProvider.fetchMetric(q.name, tsFrom, tsTo, q.tags, q.orders); metrics[i] = metrics[i].dissolveTags(q.getDissolveList(), q.aggregator); if (q.rate) { metrics[i].computeRate(); } } long loadTime = System.currentTimeMillis() - ts; JSONObject responseObj = new JSONObject(); JSONArray encodedMetrics = new JSONArray(); for (Metric metric : metrics) { encodedMetrics.add(metric.toJSONObject()); } responseObj.put("metrics", encodedMetrics); responseObj.put("loadtime", loadTime); DataTable dataTable = new DataTable(metrics); responseObj.put("datatable", dataTable.toJSONObject()); out.println(responseObj.toJSONString()); long encodingTime = System.currentTimeMillis() - ts - loadTime; logger.info("[Data] time frame: " + (tsTo - tsFrom) + "s, " + "load time: " + loadTime + "ms, " + "encoding time: " + encodingTime + "ms"); } catch (Exception e) { out.println(getErrorResponse(e)); } out.close(); }
From source file:at.uni_salzburg.cs.ckgroup.cscpp.engine.json.ActionPointQuery.java
@SuppressWarnings("unchecked") public String execute(IServletConfig config, String[] parameters) { String vehicleId = parameters[3]; String apIndexString = parameters[4]; if (vehicleId == null || "".equals(vehicleId) || apIndexString == null || !apIndexString.matches("\\d+")) { // LOG.info("action point " + parameters[0] + ":" + parameters[1] + ":" + parameters[2] + ":" + parameters[3] + ":" + parameters[4] + " Invalid query!"); return JSONValue.toJSONString("Invalid query!"); }//from w w w .j a va2 s . c o m IVirtualVehicle vehicle = vehicleMap.get(vehicleId); if (vehicle == null) { // LOG.info("action point " + parameters[0] + ":" + parameters[1] + ":" + parameters[2] + ":" + parameters[3] + ":" + parameters[4] + " Vehicle not found (anymore)!"); return JSONValue.toJSONString("Vehicle not found (anymore)!"); } int actionPointIndex = Integer.valueOf(apIndexString); if (actionPointIndex > vehicle.getTaskList().size() - 1) { // LOG.info("action point " + parameters[0] + ":" + parameters[1] + ":" + parameters[2] + ":" + parameters[3] + ":" + parameters[4] + " No such action point!"); return JSONValue.toJSONString("No such action point!"); } JSONObject props = new JSONObject(); for (Entry<Object, Object> e : vehicle.getProperties().entrySet()) { props.put((String) e.getKey(), e.getValue()); } props.put(PROP_VEHICLE_LOCAL_NAME, vehicle.getWorkDir().getName()); if (vehicle.isProgramCorrupted()) { props.put(PROP_VEHICLE_STATE, "corrupt"); } else if (vehicle.isCompleted()) { props.put(PROP_VEHICLE_STATE, "completed"); } else if (vehicle.isActive()) { props.put(PROP_VEHICLE_STATE, "active"); } else { props.put(PROP_VEHICLE_STATE, "suspended"); } ITask cmd = vehicle.getTaskList().get(actionPointIndex); props.put(PROP_VEHICLE_ACTION_POINT, cmd.getPosition()); props.put(PROP_VEHICLE_TOLERANCE, cmd.getTolerance()); JSONArray act = new JSONArray(); for (IAction a : cmd.getActionList()) { act.add(a); } props.put(PROP_VEHICLE_ACTIONS, act); // LOG.info("action point " + parameters[0] + ":" + parameters[1] + ":" + parameters[2] + ":" + parameters[3] + ":" + parameters[4] + " " + JSONValue.toJSONString(props)); return JSONValue.toJSONString(props); }
From source file:com.gti.redirects.Redirects.RedirectStorage.java
@Override public boolean add(Map redirect) { try {//from ww w . j a v a 2 s . com System.out.println("Successfully Copied JSON Object to File..."); JSONArray jsonArray = parseFile(); JSONObject redirectJson = convertRedirectToJson(redirect); jsonArray.add(redirectJson); writeJsonArray(jsonArray); return true; } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } return false; }
From source file:com.tjackiw.graylog2.plugin.jira.JiraIssue.java
public String toJSONString() { JSONObject project = new JSONObject(); project.put("key", projectKey); JSONObject issuetype = new JSONObject(); issuetype.put("name", issueType); JSONObject level = new JSONObject(); level.put("name", priority); JSONArray labelList = new JSONArray(); String[] labelArr = labels.split("\\,"); for (String s : labelArr) { labelList.add(s); }//w w w . j a va2 s . c o m JSONArray componentList = new JSONArray(); String[] compArr = components.split("\\,"); for (String s : compArr) { JSONObject hash = new JSONObject(); hash.put("name", s); componentList.add(hash); } JSONObject obj = new JSONObject(); obj.put("summary", title); obj.put("description", description); obj.put("project", project); obj.put("issuetype", issuetype); obj.put("labels", labelList); obj.put("components", componentList); obj.put("priority", level); JSONObject fields = new JSONObject(); fields.put("fields", obj); return fields.toJSONString(); }
From source file:org.uom.fit.level2.datavis.controllers.chatController.ChatController.java
@RequestMapping(value = "/allmessage", method = RequestMethod.GET) public JSONArray AllMessage() { JSONArray message = new JSONArray(); JSONArray messagelength = chatServices.getAllChatDataAllMessage(); for (int i = 0; i < messagelength.toArray().length; i++) { message.add(chatServices.getAllChatData(messagelength.get(i).toString())); }//w ww.j a va2 s. c om return message; }
From source file:com.p000ison.dev.simpleclans2.exceptions.handling.ExceptionReport.java
public JSONObject getJSONObject() { JSONObject report = new JSONObject(); report.put("plugin", name); report.put("version", version); report.put("date", date); report.put("exception_class", thrown.getClass().getName()); report.put("message", thrown.getMessage()); report.put("exception", buildThrowableJSON(thrown)); StringBuilder plugins = new StringBuilder().append('['); for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { plugins.append(plugin).append(','); }//from w ww.j av a 2s . co m plugins.deleteCharAt(plugins.length() - 1).append(']'); report.put("plugins", plugins.toString()); report.put("bukkit_version", Bukkit.getBukkitVersion()); report.put("java_version", getProperty("java.version")); report.put("os_arch", getProperty("os.arch")); report.put("os_name", getProperty("os.name")); report.put("os_version", getProperty("os.version")); if (email != null) { report.put("email", email); } JSONArray causes = new JSONArray(); Throwable cause = thrown; while ((cause = cause.getCause()) != null) { causes.add(buildThrowableJSON(cause)); } report.put("causes", causes); return report; }
From source file:com.pingidentity.adapters.idp.mobileid.restservice.MssSignatureRequestJson.java
private JSONArray buildAdditionalServices() { JSONObject object = new JSONObject(); object.put("Description", "http://mss.ficom.fi/TS102204/v1.0.0#userLang"); object.put("UserLang", buildUserLang()); JSONArray list = new JSONArray(); list.add(object); return list;//from w ww .j a v a 2s . co m }
From source file:net.sourceforge.fenixedu.presentationTier.Action.externalServices.ISTConnectDA.java
public ActionForward getExternalIds(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { final String externalIds = (String) getFromRequest(request, "externalIds"); if (doLogin(mapping, actionForm, request, response)) { JSONParser parser = new JSONParser(); final JSONArray extIdsJSONArray = (JSONArray) parser.parse(externalIds); final JSONArray jsonArrayResult = new JSONArray(); for (Object externalId : extIdsJSONArray) { jsonArrayResult.add(DomainObjectJSONSerializer .getDomainObject(FenixFramework.getDomainObject((String) externalId))); }/*from www . j a v a 2s.com*/ writeJSONObject(response, jsonArrayResult); } else { response.sendError(404, "Not authorized"); } return null; }