List of usage examples for org.json.simple JSONObject JSONObject
JSONObject
From source file:JSON.WriteProductJSON.java
public void buildJSON(String imageURL, String name, String price, String rating, String productURL) { JSONObject product1 = new JSONObject(); product1.put("image", imageURL); product1.put("name", name); product1.put("cost", price); product1.put("good", rating); product1.put("buy", productURL); details.add(product1);/*from w w w . j a v a 2s .c o m*/ }
From source file:com.oic.utils.TestValiadtion.java
public void testJsonEmpty() { JSONObject json = new JSONObject(); Validators v = new Validators(json); v.add("name", v.required()); assertFalse(v.validate());//from ww w .j ava2 s. c o m json.put("name", ""); v = new Validators(json); v.add("name", v.required()); assertFalse(v.validate()); json.put("name", "test"); v = new Validators(json); v.add("name", v.required()); assertTrue(v.validate()); }
From source file:com.intouch.action.LoginProcessor.java
@Override public JSONObject processRequest(Map<String, String[]> params) throws ServerQueryException { User user = null;//from w ww . j a v a2s . co m Gson gson; JSONObject response; response = new JSONObject(); try { isParameterExist(params, "api_key"); isParameterExist(params, "login"); isParameterExist(params, "password"); isApiKeyValid(params.get("api_key")[0]); DataHelper dataHelper = DataHelper.getInstance(); user = dataHelper.getUser(params.get("login")[0], params.get("password")[0]); if (user == null) { throw new ServerQueryException("Invalid login or password."); } } catch (ServerQueryException ex) { response.put("result", "error"); response.put("error_type", ex.getMessage()); return response; } gson = new Gson(); response = new JSONObject(); response.put("result", "success"); response.put("user", gson.toJson(user, User.class)); return response; }
From source file:com.oic.event.CheckDuplication.java
@Override public void ActionEvent(JSONObject json, WebSocketListener webSocket) { JSONObject responseJSON = new JSONObject(); responseJSON.put("method", "duplication"); Validators v = new Validators(json); v.add("username", v.required(), v.maxLength(24)); if (!v.validate()) { responseJSON.put("result", 2); return;//from www.j a v a 2 s . com } else { //?? Connection con = null; PreparedStatement ps = null; ResultSet rs = null; try { String sql = "SELECT name FROM user WHERE name = ?"; con = DatabaseConnection.getConnection(); ps = con.prepareStatement(sql); ps.setString(1, json.get("username").toString()); rs = ps.executeQuery(); if (rs.next()) {//? responseJSON.put("result", 1); } else {// webSocket.userRegister();// responseJSON.put("result", 0); } } catch (Exception e) { responseJSON.put("result", 2); e.printStackTrace(); } finally { try { rs.close(); } catch (Exception e1) { } try { ps.close(); } catch (Exception e1) { } } } webSocket.sendJson(responseJSON); }
From source file:org.jboss.aerogear.unifiedpush.test.util.AuthenticationUtils.java
public static Session login(String loginName, String password, String root) throws NullPointerException, UnexpectedResponseException { Validate.notNull(root);/*from w ww . j a v a2s . c o m*/ JSONObject jsonObject = new JSONObject(); jsonObject.put("loginName", loginName); jsonObject.put("password", password); Response response = Session.newSession(root).given().contentType(ContentTypes.json()) .header(Headers.acceptJson()).body(jsonObject.toJSONString()).post("/rest/auth/login"); // TODO should we throw or return invalid session? if (response.statusCode() == HttpStatus.SC_OK) { return new Session(root, loginName, password, response.cookies()); } else if (response.statusCode() == HttpStatus.SC_FORBIDDEN) { throw new ExpiredPasswordException(response); } else if (response.statusCode() == HttpStatus.SC_UNAUTHORIZED) { throw new InvalidPasswordException(response); } else { // This should never happen throw new UnexpectedResponseException(response); } }
From source file:com.au.splashinc.JControl.Load.DarkForcesJsonLoader.java
public DarkForcesJsonLoader(String location) { super(location); json = new JSONObject(); }
From source file:com.intouch.action.RegistrationProcessor.java
@Override public JSONObject processRequest(Map<String, String[]> params) throws ServerQueryException { JSONObject response;//from ww w. j a va 2 s.c o m response = new JSONObject(); DataHelper dataHelper = null; try { isParameterExist(params, "api_key"); isParameterExist(params, "login"); isParameterExist(params, "password"); isParameterExist(params, "first_name"); isParameterExist(params, "last_name"); isApiKeyValid(params.get("api_key")[0]); dataHelper = DataHelper.getInstance(); if (dataHelper.getUserByLogin(params.get("login")[0]) != null) { throw new ServerQueryException("User with login " + params.get("login")[0] + "already exist."); } } catch (ServerQueryException ex) { response.put("result", "error"); response.put("error_type", ex.getMessage()); return response; } User user = new User(params.get("first_name")[0], params.get("last_name")[0], params.get("login")[0], params.get("password")[0], new Date(), new Date(), UUID.randomUUID().toString()); dataHelper.createNewUser(user); Gson gson = new Gson(); response.put("result", "success"); response.put("user", gson.toJson(user, User.class)); return response; }
From source file:com.oic.event.GetUserInfo.java
@Override public void ActionEvent(JSONObject json, WebSocketListener webSocket) { JSONObject responseJSON = new JSONObject(); responseJSON.put("method", "getuserinfo"); OicCharacter c = webSocket.getCharacter(); long userid = c.getUserId(); int mapid = c.getMapid(); if (validation(json)) { userid = Long.parseLong(json.get("userid").toString()); mapid = Integer.parseInt(json.get("mapid").toString()); }/*from w ww . j a va 2s . co m*/ System.out.println("mapid : " + mapid); System.out.println("userid : " + userid); try { getUserinfo(responseJSON, userid, mapid); } catch (NullPointerException e) { responseJSON.put("status", 1); } webSocket.sendJson(responseJSON); }
From source file:com.oic.event.map.GetMapUserList.java
@Override public void ActionEvent(JSONObject json, WebSocketListener webSocket) { JSONObject responseJSON = new JSONObject(); responseJSON.put("method", "getmapuserlist"); if (!validation(json)) { responseJSON.put("status", 1); webSocket.sendJson(responseJSON); return;/*www . java 2 s . c o m*/ } int mapid = Integer.parseInt(json.get("mapid").toString()); MapFactory factory = MapFactory.getInstance(); OicMap map = factory.getMap(mapid); JSONArray charactersJSON = new JSONArray(); try { for (Long userid : map.getUserIdList()) { charactersJSON.add(userid); } } catch (NullPointerException ex) { } responseJSON.put("userlist", charactersJSON); webSocket.sendJson(responseJSON); }
From source file:com.healthcit.analytics.utils.ExcelExportUtils.java
@SuppressWarnings("unchecked") public static void streamVisualizationDataAsExcelFormat(OutputStream out, JSONObject data) { // Get the array of columns JSONArray jsonColumnArray = (JSONArray) data.get(COLUMNS_JSON_KEY); String[] excelColumnArray = new String[jsonColumnArray.size()]; for (int index = 0; index < excelColumnArray.length; ++index) { excelColumnArray[index] = (String) ((JSONObject) jsonColumnArray.get(index)).get(LABEL_JSON_KEY); }/* w w w. j av a2 s .c om*/ // Get the array of rows JSONArray jsonRowArray = (JSONArray) data.get(ROWS_JSON_KEY); JSONArray excelRowArray = new JSONArray(); Iterator<JSONObject> jsonRowIterator = jsonRowArray.iterator(); while (jsonRowIterator.hasNext()) { JSONArray rowCell = (JSONArray) jsonRowIterator.next().get(ROWCELL_JSON_KEY); JSONObject excelRowObj = new JSONObject(); for (int index = 0; index < rowCell.size(); ++index) { excelRowObj.put(excelColumnArray[index], ((JSONObject) rowCell.get(index)).get(ROWCELLVALUE_JSON_KEY)); } excelRowArray.add(excelRowObj); } // build the Excel outputstream Json2Excel.build(out, excelRowArray.toJSONString(), excelColumnArray); }