/** * Produce a JSONObject by combining a JSONArray of names with the values of this JSONArray. * * @param names A JSONArray containing a list of key strings. These will be paired with the * values. * @return A JSONObject, or null if there are no names or if this JSONArray has no values. * @throws JSONException If any of the names are null. */ public JSONObject toJSONObject(JSONArray names) throws JSONException { if (names == null || names.length() == 0 || length() == 0) { return null; } JSONObject jo = new JSONObject(); for (int i = 0; i < names.length(); i += 1) { jo.put(names.getString(i), this.opt(i)); } return jo; }
/** * Write the contents of the JSONArray as JSON text to a writer. For compactness, no whitespace is * added. * * <p>Warning: This method assumes that the signalData structure is acyclical. * * @return The writer. * @throws JSONException */ public Writer write(Writer writer) throws JSONException { try { boolean b = false; int len = length(); writer.write('['); for (int i = 0; i < len; i += 1) { if (b) { writer.write(','); } Object v = this.myArrayList.get(i); if (v instanceof JSONObject) { ((JSONObject) v).write(writer); } else if (v instanceof JSONArray) { ((JSONArray) v).write(writer); } else { writer.write(JSONObject.valueToString(v)); } b = true; } writer.write(']'); return writer; } catch (IOException e) { throw new JSONException(e); } }
public void retrieveCity() throws Exception { ClientRequest request = new ClientRequest("http://api.geonames.org/postalCodeSearchJSON"); request.queryParameter("postalcode", order.getZip()); request.queryParameter("country", "DE"); request.queryParameter("username", "camunda"); request.accept("application/json"); ClientResponse<String> response = request.get(String.class); JSONObject jsonObject = new JSONObject(response.getEntity()); JSONArray jsonArray = jsonObject.getJSONArray("postalCodes"); if (jsonArray.length() == 0) { throw new BpmnError("CITY_NOT_FOUND"); } String city = jsonArray.getJSONObject(0).getString("placeName"); log.info("### city retrieved: " + city); order.setCity(city); }