List of usage examples for org.json.simple JSONObject toJSONString
public String toJSONString()
From source file:com.criticalsoftware.mobics.presentation.util.GeolocationUtil.java
/** * Get the full address name/* w ww . ja va 2 s . c om*/ * * @param latitude * @param longitude * @return address string */ public static String getAddressFromCoordinates(String latitude, String longitude) { String address = null; String url = MessageFormat.format(GEOLOCATION_URL, Configuration.INSTANCE.getGeolocationServer(), latitude, longitude); HttpMethod method = new GetMethod(url); try { if (LOGGER.isTraceEnabled()) { LOGGER.trace("Making reverse geolocation call to: {}", url); } int statusCode = new HttpClient().executeMethod(method); if (statusCode == HttpStatus.SC_OK) { byte[] responseBody = readResponse(method); JSONObject jsonObject = (JSONObject) new JSONParser().parse(new String(responseBody)); if (LOGGER.isTraceEnabled()) { LOGGER.trace(jsonObject.toJSONString()); } address = getFormattedAddress(jsonObject); } else { LOGGER.warn("Recived a HTTP status {}. Response was not good from {}", statusCode, url); } } catch (HttpException e) { LOGGER.error("Error while making call.", e); } catch (IOException e) { LOGGER.error("Error while reading the response.", e); } catch (ParseException e) { LOGGER.error("Error while parsing json response.", e); } return address; }
From source file:kltn.geocoding.Geocoding.java
private static Geometry getBoundary(String s) throws MalformedURLException, IOException, org.json.simple.parser.ParseException { String link = "https://maps.googleapis.com/maps/api/geocode/json?&key=AIzaSyALCgmmer3Cht-mFQiaJC9yoWdSqvfdAiM"; link = link + "&address=" + URLEncoder.encode(s); URL url = new URL(link); HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection(); InputStream is = httpsCon.getInputStream(); StringWriter writer = new StringWriter(); IOUtils.copy(is, writer, "UTF-8"); String jsonString = writer.toString(); JSONParser parse = new JSONParser(); Object obj = parse.parse(jsonString); JSONObject jsonObject = (JSONObject) obj; System.out.println(s);/*www .ja v a2 s . c o m*/ System.out.println(jsonObject.toJSONString()); JSONArray resultArr = (JSONArray) jsonObject.get("results"); Object resultObject = parse.parse(resultArr.get(0).toString()); JSONObject resultJsonObject = (JSONObject) resultObject; Object geoObject = parse.parse(resultJsonObject.get("geometry").toString()); JSONObject geoJsonObject = (JSONObject) geoObject; if (!geoJsonObject.containsKey("bounds")) { return null; } Object boundObject = parse.parse(geoJsonObject.get("bounds").toString()); JSONObject boundJsonObject = (JSONObject) boundObject; // System.out.println(boundJsonObject.toJSONString()); Object southwest = parse.parse(boundJsonObject.get("southwest").toString()); JSONObject southwestJson = (JSONObject) southwest; String southwestLat = southwestJson.get("lat").toString(); String southwestLong = southwestJson.get("lng").toString(); Object northeast = parse.parse(boundJsonObject.get("northeast").toString()); JSONObject northeastJson = (JSONObject) northeast; String northeastLat = northeastJson.get("lat").toString(); String northeastLong = northeastJson.get("lng").toString(); String polygon = "POLYGON((" + southwestLong.trim() + " " + northeastLat.trim() + "," + northeastLong.trim() + " " + northeastLat.trim() + "," + northeastLong.trim() + " " + southwestLat.trim() + "," + southwestLong.trim() + " " + southwestLat.trim() + "," + southwestLong.trim() + " " + northeastLat.trim() + "))"; Geometry geo = wktToGeometry(polygon); return geo; }
From source file:msuresh.raftdistdb.RaftCluster.java
private static void UpdatePortNumber() { try {//from www.j av a 2s .c o m JSONParser parser = new JSONParser(); Object obj = parser.parse(new FileReader(Constants.STATE_LOCATION + "global.info")); JSONObject jsonObject = (JSONObject) obj; jsonObject.put("currentCount", portId); try (FileWriter file = new FileWriter(Constants.STATE_LOCATION + "global.info")) { file.write(jsonObject.toJSONString()); } } catch (Exception e) { } }
From source file:com.sugarcrm.candybean.webservices.WS.java
/** * Private helper method to abstract creating a POST/PUT request. * Side Effect: Adds the body to the request * * @param request A PUT or POST request * @param body Map of Key Value pairs * @param contentType The intended content type of the body *///w w w . j a v a2s . co m protected static void addBodyToRequest(HttpEntityEnclosingRequestBase request, Map<String, Object> body, ContentType contentType) { if (body != null) { if (contentType == ContentType.MULTIPART_FORM_DATA) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); for (Map.Entry<String, Object> entry : body.entrySet()) { builder.addTextBody(entry.getKey(), (String) entry.getValue()); } request.setEntity(builder.build()); } else { JSONObject jsonBody = new JSONObject(body); StringEntity strBody = new StringEntity(jsonBody.toJSONString(), ContentType.APPLICATION_JSON); request.setEntity(strBody); } } }
From source file:com.wso2.raspberrypi.apicalls.APICall.java
public static void registerDevice(String deviceID, String zoneID) { String url = apiURLPrefix + "/conferences/2/iot/scannerZones/byScannerUUID"; Token token = getToken();// ww w .j a v a 2s . c om if (token != null) { HttpClient httpClient = new HttpClient(); JSONObject json = new JSONObject(); json.put("rfidScannerUUID", deviceID); json.put("zoneId", zoneID); try { HttpResponse httpResponse = httpClient.doPost(url, "Bearer " + token.getAccessToken(), json.toJSONString(), "application/json"); int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode != 200) { log.error("Device to Zone registration failed. HTTP Status Code:" + statusCode); } } catch (IOException e) { log.error("", e); } } }
From source file:co.alligo.toasted.routes.ServerList.java
public static String serverList(Request req, Response res, Servers servers) { JSONObject response = new JSONObject(); JSONObject result = new JSONObject(); res.header("Content-Type", "application/json"); response.put("listVersion", 1); result.put("code", 0); result.put("msg", "OK"); result.put("servers", servers.getServers()); response.put("result", result); return (String) response.toJSONString(); }
From source file:com.jilk.ros.rosbridge.implementation.JSON.java
/** * Translates a Message recursively into JSON. Normally the Message is also an * Operation, but it does not have to be. The caller constructs a complete * message using @Operation and @Message types. This includes situations * where one or more fields are marked to be turned into arrays, using @AsArray. * @param m the @Message object to be recursively translated. * @return the complete JSON string./*from w w w .j a va 2s.c om*/ */ public static String toJSON(Message m) { JSONObject jo = convertObjectToJSONObject(m); // Object to JSON-Simple return jo.toJSONString(); // JSON-Simple to string }
From source file:com.wso2.rfid.apicalls.APICall.java
public static void userCheckin(String deviceID, String rfid, String url, String consumerKey, String consumerSecret) throws UserCheckinException { // String url = "https://gateway.apicloud.cloudpreview.wso2.com:8243/t/indikas.com/wso2coniot/1.0.0/conferences/2/userCheckIn"; Token token = getToken(consumerKey, consumerSecret); if (token != null) { HttpClient httpClient = new HttpClient(); JSONObject json = new JSONObject(); json.put("uuid", deviceID); json.put("rfid", rfid); try {/*from w w w . j a v a 2 s. co m*/ HttpResponse httpResponse = httpClient.doPost(url, "Bearer " + token.getAccessToken(), json.toJSONString(), "application/json"); int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode != 200) { throw new UserCheckinException( "User checkin failed. HTTP Status Code:" + statusCode + ", RFID: " + rfid + ", CK=" + consumerKey + ", CS=" + consumerSecret + ", URL=" + url); } } catch (IOException e) { log.error("", e); } } }
From source file:fr.zcraft.zlib.components.rawtext.RawTextTest.java
static private void assertJSON(RawText part, String jsonMessage) { JSONObject obj; try {/*from w ww.j a v a 2 s . co m*/ obj = (JSONObject) new JSONParser().parse(jsonMessage); } catch (ParseException e) { throw new RuntimeException(e); } Assert.assertEquals(obj.toJSONString(), part.toJSONString()); }
From source file:model.Post_store.java
public static void setlastid(int lastid) { JSONObject obj = new JSONObject(); obj.put("lastid", lastid); try (FileWriter file = new FileWriter(root + "posts/lastid.json");) { file.write(obj.toJSONString()); file.flush();/*from w w w. j a v a2 s .c o m*/ file.close(); } catch (IOException ex) { System.out.println(ex); } }