List of usage examples for org.json.simple JSONArray add
public boolean add(E e)
From source file:fr.nantes.web.quizz.servlets.Addadmin.java
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String idadmin = request.getParameter("idadmin"); JSONArray result = new JSONArray(); JSONObject map = new JSONObject(); if (idadmin.length() == 21 && Requetesdatastore.addadmin(idadmin) == idadmin) { map.put("idadmin", idadmin); result.add(map); } else {/*from w w w . j ava 2s . co m*/ map.put("idadmin", -100); result.add(map); } response.setContentType("application/json;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { out.println(result); } }
From source file:be.iminds.aiolos.ui.ComponentServlet.java
@SuppressWarnings("unchecked") @Override/*from w w w .j a v a 2s. com*/ protected void writeJSON(Writer w, Locale locale) throws IOException { Collection<ComponentDescription> components = getComponentsInfo(); final Object[] status = getStatusLine(components); final JSONObject obj = new JSONObject(); JSONArray stat = new JSONArray(); for (int i = 0; i < status.length; i++) stat.add(status[i]); obj.put("status", stat); final JSONArray list = new JSONArray(); for (ComponentDescription component : components) { JSONObject jsonComponent = new JSONObject(); jsonComponent.put("id", component.componentId); jsonComponent.put("version", component.version); JSONArray jsonNodes = new JSONArray(); jsonNodes.addAll(component.nodes); jsonComponent.put("nodes", jsonNodes); list.add(jsonComponent); } obj.put("components", list); w.write(obj.toJSONString()); }
From source file:gov.nih.nci.ispy.web.ajax.IdLookup.java
public String lookup(String ids) { String results = ""; //clean the input and validate String inputString = ids.trim().replace(" ", ""); String[] st = StringUtils.split(inputString, ","); //construct the inputlist List<String> inputList = new ArrayList<String>(); inputList = Arrays.asList(st); //pass the input list to the service List<RegistrantInfo> entries = idMapper.getMapperEntriesForIds(inputList); //process the results for return to presentation //Document document = DocumentHelper.createDocument(); //Element container = document.addElement("div"); JSONArray regs = new JSONArray(); //make an array of registrants regs.add(inputString); //Element report = document.addElement( "table" ).addAttribute("name", inputString); for (RegistrantInfo entry : entries) { //Element reg = report.addElement( "registrant" ).addAttribute("regId", entry.getRegistrationId()); //for each registrant make an array of samples JSONArray sams = new JSONArray(); for (SampleInfo sampleInfo : entry.getAssociatedSamples()) { JSONObject sam = new JSONObject(); sam.put("regId", entry.getRegistrationId()); sam.put("labtrackId", sampleInfo.getLabtrackId()); sam.put("timePoint", String.valueOf(sampleInfo.getTimepoint())); sam.put("coreType", String.valueOf(sampleInfo.getCoreType())); sam.put("sectionInfo", String.valueOf(sampleInfo.getSectionInfo())); /*/* w w w . j a v a 2 s .c om*/ Element row = reg.addElement( "sample" ); Element cell = row.addElement( "regId" ); cell.addText(entry.getRegistrationId()); cell = null; cell = row.addElement( "labtrackId" ); cell.addText(sampleInfo.getLabtrackId()); cell = null; cell = row.addElement( "timePoint" ); cell.addText(String.valueOf(sampleInfo.getTimepoint())); cell = null; cell = row.addElement( "coreType" ); cell.addText(String.valueOf(sampleInfo.getCoreType())); cell = null; cell = row.addElement( "sectionInfo" ); cell.addText(String.valueOf(sampleInfo.getSectionInfo())); */ sams.add(sam); } regs.add(sams); } /* for (RegistrantInfo entry:entries) { results += "[\"" + entry.getRegistrationId() + "\"," ; for(SampleInfo sampleInfo : entry.getAssociatedSamples()) { } } */ //return document; return regs.toString(); }
From source file:com.juniform.JUniformPackerJSON.java
@SuppressWarnings("unchecked") private Object _toJSON(JUniformObject object) { if (object == null) { return null; } else if (object.isMapValue()) { JSONObject jsonMap = new JSONObject(); Iterator<Map.Entry<Object, JUniformObject>> it = object.entrySet().iterator(); while (it.hasNext()) { Map.Entry<Object, JUniformObject> pair = it.next(); jsonMap.put(pair.getKey(), this._toJSON(pair.getValue())); }/* www.jav a2 s . c o m*/ return jsonMap; } else if (object.isArrayValue()) { JSONArray jsonArray = new JSONArray(); Iterator<JUniformObject> it = object.iterator(); while (it.hasNext()) { jsonArray.add(this._toJSON(it.next())); } return jsonArray; } else { return object.getValue(); } }
From source file:com.googlecode.fascinator.redbox.plugins.curation.external.FormDataParser.java
/** * Get a child JSON Object from an incoming JSON array. If the child does * not exist it will be created, along with any smaller index values. The * index is expected to be 1 based (like form data). * //from www . j a va2 s. c om * It is only valid for form arrays to hold JSON Objects. * * @param array The incoming array we are to look inside * @param index The child index we are looking for (1 based) * @return JsonObject The child we found or created * @throws IOException if anything other than an object is found, or an * invalid index is provided */ private static JsonObject getObject(JSONArray array, int index) throws IOException { // We can't just jam an entry into the array without // checking that earlier indexes exist. Also we need // to account for 0 versus 1 based indexing. // Index changed to 0 based if (index < 1) { throw new IOException("Invalid index value provided in form data."); } index -= 1; // Nice and easy, it already exists if (array.size() > index) { Object object = array.get(index); if (object instanceof JsonObject) { return (JsonObject) object; } throw new IOException("Non-Object found in array!"); // Slightly more annoying, we need to fill in // all the indices up to this point } else { for (int i = array.size(); i <= index; i++) { JsonObject object = new JsonObject(); array.add(object); } return (JsonObject) array.get(index); } }
From source file:gr.iit.demokritos.cru.cps.api.SubmitUserInformalEvaluationList.java
public JSONObject processRequest() throws IOException, Exception { String response_code = "e0"; long user_id = 0; String[] users = users_list.split(";"); ArrayList<Long> users_id = new ArrayList<Long>(); for (int i = 0; i < users.length; i++) { users_id.add(Long.parseLong(users[i])); }/*from www . j a va2 s. co m*/ String location = (String) properties.get("location"); String database_name = (String) properties.get("database_name"); String username = (String) properties.get("username"); String password = (String) properties.get("password"); MySQLConnector mysql = new MySQLConnector(location, database_name, username, password); try { user_id = Long.parseLong(evaluator_id); Connection connection = mysql.connectToCPSDatabase(); Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT window FROM windows WHERE current_window=1"); int window = -1; while (rs.next()) { window = rs.getInt(1); } rs.close(); UserManager um = new UserManager(Long.parseLong(application_key), user_id, users_id); CreativityExhibitModelController cemc = new CreativityExhibitModelController(window, Long.parseLong(application_key), user_id, users_id); boolean isvalid = false; isvalid = um.validateClientApplication(mysql); if (isvalid == true) { isvalid = um.validateUser(mysql); if (isvalid == true) { Iterator it = users_id.iterator(); while (it.hasNext()) { um.setUser_id((Long) it.next()); isvalid = um.validateUser(mysql); if (isvalid == false) { break; } } } else { response_code = "e102"; } if (isvalid == true) { cemc.storeEvaluation(mysql); response_code = "OK"; } else { response_code = "e102"; } } else { response_code = "e101"; } } catch (NumberFormatException ex) { response_code = "e101"; Logger.getLogger(CreateUser.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(CreateUser.class.getName()).log(Level.SEVERE, null, ex); } JSONArray list = new JSONArray(); for (Long temp_user : users_id) { list.add(Long.toString(temp_user)); } JSONObject obj = new JSONObject(); obj.put("application_key", application_key); obj.put("user_id", Long.toString(user_id)); obj.put("users_id", list); obj.put("response_code", response_code); return obj; }
From source file:net.sourceforge.fenixedu.presentationTier.Action.externalServices.ISTConnectDA.java
public ActionForward getBasicUserData(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { if (doLogin(mapping, actionForm, request, response)) { final String istID = (String) getFromRequest(request, "istID"); final Person person = Person.readPersonByUsername(istID); final JSONObject jsonObject = new JSONObject(); if (person != null) { jsonObject.put("externalId", person.getExternalId()); jsonObject.put("className", person.getClass().getName()); jsonObject.put("email", person.getEmailForSendingEmails()); jsonObject.put("partyName", person.getPartyName().toString()); jsonObject.put("nickname", person.getNickname()); JSONArray jsonList = new JSONArray(); for (final net.sourceforge.fenixedu.domain.Role role : person.getPersonRolesSet()) { jsonList.add(role.getRoleType().getName()); }//from w w w. ja v a2 s . co m jsonObject.put("roles", jsonList); } writeJSONObject(response, jsonObject); } else { response.sendError(404, "Not authorized"); } return null; }
From source file:at.uni_salzburg.cs.ckgroup.cscpp.mapper.PolygonZone.java
@SuppressWarnings("unchecked") @Override/*from w w w. j ava2 s . c o m*/ public String toJSONString() { JSONObject o = new JSONObject(); JSONArray a = new JSONArray(); for (TwoTuple t : vertices) { a.add(t); } o.put("type", "polygon"); o.put("vertices", a); o.put("depot", new TwoTuple(getDepotPosition().getLatitude(), getDepotPosition().getLongitude())); return o.toJSONString(); }
From source file:com.p000ison.dev.simpleclans2.exceptions.handling.ExceptionReporterTask.java
@Override @SuppressWarnings("unchecked") public void run() { if (queue.isEmpty()) { return;/*from w w w . ja va 2s .c o m*/ } JSONArray reports = new JSONArray(); ExceptionReport report; int i = 0; while ((report = queue.poll()) != null && i <= MAX_REPORTS_PER_PUSH) { i++; reports.add(report.getJSONObject()); } try { PHPConnection connection = new PHPConnection(PROTOCOL, HOST, PORT, FILE, true); connection.write("report=" + reports.toJSONString()); String response = connection.read(); if (response != null && !response.isEmpty()) { throw new IOException("Failed at pushing error reports: " + response); } } catch (IOException e) { Logging.debug(e, false); } }
From source file:hoot.services.nativeInterfaces.ProcessStreamInterfaceTest.java
@Test @Category(UnitTest.class) public void testcreateCmd() throws Exception { String jobIdStr = java.util.UUID.randomUUID().toString(); JSONArray args = new JSONArray(); JSONObject translation = new JSONObject(); translation.put("translation", "/test/loc/translation.js"); args.add(translation); JSONObject output = new JSONObject(); output.put("output", "/test/loc/out.osm"); args.add(output);//from www . ja va2 s . c o m JSONObject input = new JSONObject(); input.put("input", "/test/loc/input.shp"); args.add(output); JSONObject command = new JSONObject(); command.put("exectype", "hoot"); command.put("exec", "ogr2osm"); command.put("params", args); ProcessStreamInterface ps = new ProcessStreamInterface(); Class[] cArg = new Class[1]; cArg[0] = JSONObject.class; Method method = ProcessStreamInterface.class.getDeclaredMethod("createCmdArray", cArg); method.setAccessible(true); String[] ret = (String[]) method.invoke(ps, command); String commandStr = ArrayUtils.toString(ret); String expected = "{hoot,--ogr2osm,/test/loc/translation.js,/test/loc/out.osm,/test/loc/out.osm}"; Assert.assertEquals(expected, commandStr); }