List of usage examples for org.json.simple JSONObject toString
@Override
public String toString()
From source file:hobbyshare.testclient.RestService_TestClient.java
public void test_Login() { HttpClient httpClient = HttpClientBuilder.create().build(); JSONObject loginAttempt = new JSONObject(); loginAttempt.put("password", "1234"); loginAttempt.put("userName", "77_username"); try {/*from w ww .j ava2 s . co m*/ HttpPost request = new HttpPost("http://localhost:8095/login"); StringEntity params = new StringEntity(loginAttempt.toString()); request.addHeader("content-type", "application/json"); request.setEntity(params); HttpResponse response = httpClient.execute(request); System.out.println("---Testing Login----"); System.out.println(response.toString()); HttpEntity entity = response.getEntity(); String responseString = EntityUtils.toString(entity, "UTF-8"); System.out.println(responseString); System.out.println("----End of login Test----"); } catch (Exception ex) { // handle exception here } finally { httpClient.getConnectionManager().shutdown(); } }
From source file:com.oic.net.WebSocketListener.java
/** * ????/*from w ww . ja v a 2s . co m*/ * * @param method * @param json * @param webSocketListener */ public void selectMessage(String method, JSONObject json, WebSocketListener webSocketListener) { if (method == null) { //?????? LOG.log(Level.WARNING, "Message Method is NULL : {0}", json.toString()); return; } else if (method.equals("cmd")) { // new CmdEvent().ActionEvent(json, webSocketListener); } else if ((login == LoginStatus.NOLOGIN || login == LoginStatus.REGISTER) && method.equals("duplication")) { /* ????Status?REGISTER?? */ new CheckDuplication().ActionEvent(json, webSocketListener); } else if (login == LoginStatus.NOLOGIN && method.equals("login")) { /* */ new LoginHandler().ActionEvent(json, webSocketListener); //new LoginHandler().ActionEvent(json, this);//? } else if (login == LoginStatus.REGISTER && method.equals("setprofile")) { /* ????? */ new RegisterProfile().ActionEvent(json, webSocketListener); } else if (login == LoginStatus.LOGIN) { LoginEvent.execEvent(method, json, webSocketListener); } }
From source file:com.ibm.storlet.common.StorletContainerHandle.java
@SuppressWarnings("unchecked") public StorletObjectOutputStream getObjectOutputStream(String objectName) throws StorletException { StorletObjectOutputStream objectStream = null; String key = containerName + objectName + new Date().getTime(); JSONObject jRequestObj = new JSONObject(); jRequestObj.put("object_name", objectName); jRequestObj.put("container_name", containerName); jRequestObj.put("key", key); ObjectRequestEntry requestEntry = requestTable.Insert(key); try {// www. j a va2 s. c om stream.write(jRequestObj.toString().getBytes()); } catch (IOException e) { throw new StorletException("Failed to serialize object descriptor request " + e.toString()); } try { objectStream = requestEntry.get(); } catch (InterruptedException e) { throw new StorletException("Exception while waiting for request entry" + e.getMessage()); } requestTable.Remove(key); return objectStream; }
From source file:chat.com.client.ChatClient.java
private void formWindowClosing(java.awt.event.WindowEvent evt) { JSONObject jsonClientOut = new JSONObject(); jsonClientOut.put("id", id); jsonClientOut.put("type", "clientout"); String json = jsonClientOut.toString(); writer.print(json);/*w w w . j av a 2s . co m*/ writer.flush(); try { socket.close(); } catch (IOException ex) { Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex); } System.exit(0); }
From source file:com.owly.srv.RemoteBasicStat.java
/** * Method which adds in to the object all fields related to the JSON object * obtained from the remote Server HTTP response. Here is an example of this * JSON Object :/*from w w w . j a v a 2s. c om*/ * {"StatType":"TopCpu","Date":"2013-05-16 09:39:17","Host":"centos-63", * "Metrics" * :{"cpu_idle":97.6,"cpu_steal":0.0,"cpu_wait":0.0,"cpu_low_priority" * :0.0,"cpu_software_interrupts" * :0.0,"cpu_hardware_interrupts":0.0,"cpu_kernel":0.0,"cpu_user":2.4}, * "MetricType":"SystemStat"} * * @param jsonObject */ public void setRmtSeverfromJSONObject(JSONObject jsonObject) { Logger log = Logger.getLogger(RemoteBasicStat.class); log.debug("Calling setRmtSeverfromJSONObject"); log.debug("JSON object to analyze : " + jsonObject.toString()); this.setTypeOfStat((String) jsonObject.get("StatType")); log.debug(" Type Stat : " + this.getTypeOfStat()); if (this.getTypeOfStat().equals("NOK")) { log.error("Metric received is not OK, check in client side"); } else { this.setHostname((String) jsonObject.get("Host")); log.debug(" Hostname : " + this.getHostname()); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String strDateRemote = (String) jsonObject.get("Date"); try { Date dateRemote = dateFormat.parse(strDateRemote); this.setRmtServerDate(dateRemote); log.debug("Remote date is " + dateFormat.format(this.getDateRmtServerDate())); } catch (ParseException e) { log.error("Parse date Error : " + e.getMessage()); log.error("Exception ::", e); } this.setMetrics((JSONObject) jsonObject.get("Metrics")); log.debug(" Metrics : " + this.getMetrics().toString()); this.setMetricType((String) jsonObject.get("MetricType")); log.debug(" Type of Metric : " + this.getMetricType()); } }
From source file:com.aerospike.load.Parser.java
/** * //from w w w . ja va 2 s. c o m * @param jobj JSON object which contains json formatted data * @param jobjName JSON object name for which it asks for * @return ColumnDefinition object */ public static ColumnDefinition getColumnDefs(JSONObject jobj, String jobjName) throws Exception { String binNameHeader = null; String binValueHeader = null; boolean staticName = true; boolean staticValue = true; String srcType = null; String dstType = null; String encoding = null; int binNamePos = -1; int binValuePos = -1; String columnName = null; String jsonPath = null; if (Constants.KEY.equalsIgnoreCase(jobjName) || Constants.SET.equalsIgnoreCase(jobjName)) { // Get key/set location if ((jobj.get(Constants.COLUMN_POSITION)) == null) { if ((columnName = (String) (jobj.get(Constants.COLUMN_NAME))) == null) { if ((jsonPath = (String) (jobj.get(Constants.JSON_PATH))) == null) { log.error("Specify proper key/set mapping in config file for " + jobjName + ":" + jobj.toString()); } else { //TODO for json format } } else { binNameHeader = jobjName; binValueHeader = columnName; } } else { binNameHeader = jobjName; binValuePos = (Integer.parseInt(jobj.get(Constants.COLUMN_POSITION).toString()) - 1); } // Get key type if (Constants.KEY.equalsIgnoreCase(jobjName)) { srcType = (String) jobj.get(Constants.TYPE); } // Default set type is string if (Constants.SET.equalsIgnoreCase(jobjName)) { srcType = (String) jobj.get(Constants.TYPE); if (srcType == null) srcType = "string"; } //key and set value are always dynamic staticValue = false; } else if (Constants.BINLIST.equalsIgnoreCase(jobjName)) { Object obj; // Get name information of bin if ((obj = jobj.get(Constants.NAME)) != null) { if (obj instanceof JSONObject) { staticName = false; JSONObject nameObj = (JSONObject) obj; if ((nameObj.get(Constants.COLUMN_POSITION)) == null) { if ((columnName = (String) (nameObj.get(Constants.COLUMN_NAME))) == null) { if ((jsonPath = (String) (nameObj.get(Constants.JSON_PATH))) == null) { log.error("Specify proper bin name mapping in config file for " + jobjName + ":" + jobj.toString()); } else { //TODO for json format } } else binNameHeader = columnName; } else { binNamePos = (Integer.parseInt(nameObj.get(Constants.COLUMN_POSITION).toString()) - 1); } } else { binNameHeader = (String) obj;//default name } } else { log.error(Constants.NAME + " key is missing object:" + jobj.toString()); } // Get value information of bin JSONObject valueObj; if ((obj = jobj.get(Constants.VALUE)) != null) { if (obj instanceof JSONObject) { staticValue = false; valueObj = (JSONObject) obj; if ((valueObj.get(Constants.COLUMN_POSITION)) == null) { if ((columnName = (String) (valueObj.get(Constants.COLUMN_NAME))) == null) { if ((jsonPath = (String) (valueObj.get(Constants.JSON_PATH))) == null) { log.error("Specify proper bin value mapping in config file for " + jobjName); } else { //TODO for json } } else { binValueHeader = columnName; } } else { binValuePos = (Integer.parseInt(valueObj.get(Constants.COLUMN_POSITION).toString()) - 1); } // Get src data type if ((srcType = (String) (valueObj.get(Constants.TYPE))) == null) { log.error(Constants.TYPE + " key is missing in bin object:" + jobj.toString()); } // Get destination type conversion for src data format if ((dstType = (String) (valueObj.get(Constants.DST_TYPE))) != null) { //log.error(Constants.DST_TYPE + " key is missing in bin object:"+ jobj.toString()); } // Get encoding format if ((encoding = (String) (valueObj.get(Constants.ENCODING))) == null) { //log.error(Constants.ENCODING + " key is missing in bin object:"+ jobj.toString()); } } else { binValueHeader = (String) obj;// default value } } else { log.error(Constants.VALUE + " key is missing in bin object:" + jobj.toString()); } } ColumnDefinition colDef = new ColumnDefinition(binNameHeader, binValueHeader, staticName, staticValue, srcType, dstType, encoding, binNamePos, binValuePos, columnName, jsonPath); return colDef; }
From source file:chat.com.server.ChatServer.java
private void updateUserOnline() { int s = map.size(); JSONArray arr = new JSONArray(); for (String key : map.keySet()) { // add user online arr.add(key);//from ww w. j av a 2s . c o m } JSONObject jsonUpdateUser = new JSONObject(); jsonUpdateUser.put("type", "userOnline"); jsonUpdateUser.put("listUser", arr); String json = jsonUpdateUser.toString(); for (String key : map.keySet()) { // send message to all PrintWriter writer = map.get(key); writer.println(json); writer.flush(); } }
From source file:control.ProcesoVertimientosServlets.EliminarProcesoVertimientos.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request/*from w ww . j av a 2s. c om*/ * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { JSONObject resp = new JSONObject(); try { int codigo = Integer.parseInt(request.getParameter("codigo")); ProcesoVertimientos manager = new ProcesoVertimientos(); resp = manager.eliminarProcesoVertimiento(codigo); response.setContentType("application/json"); response.getWriter().write(resp.toString()); } catch (Exception ex) { resp.put("error", 0); response.getWriter().write(resp.toString()); } }
From source file:at.uni_salzburg.cs.ckgroup.cscpp.mapper.course.WayPoint.java
@SuppressWarnings("unchecked") @Override/*from w w w .j a v a 2 s .c om*/ public String toJSONString() { JSONObject obj = new JSONObject(); obj.put("latitude", Double.valueOf(point.getLatitude())); obj.put("longitude", Double.valueOf(point.getLongitude())); obj.put("altitude", Double.valueOf(point.getAltitude())); obj.put("precision", Double.valueOf(precision)); obj.put("velocity", Double.valueOf(velocity)); return obj.toString(); }
From source file:control.ProcesoVertimientosServlets.SeleccionarInformacionProcesoSeco.java
private void getInformacionProcesoSeco(HttpServletRequest request, HttpServletResponse response) throws IOException { try {/* www .j a va 2s. c o m*/ int codigoProceso = Integer.parseInt(request.getParameter("codigoProceso")); JSONObject jsonObject = new JSONObject(); InformeProcesoSeco manager = new InformeProcesoSeco(); jsonObject = manager.getInformacionProcesoSeco(codigoProceso); //Armamos la respuesta JSON y la enviamos response.setContentType("application/json"); response.getWriter().write(jsonObject.toString()); } catch (SQLException ex) { } }