/** * Produce a JSONArray containing the names of the elements of this * JSONObject. * * @return A JSONArray containing the key strings, or null if the JSONObject * is empty. */ public JSONArray names() { JSONArray ja = new JSONArray(); Iterator<String> keys = keys(); while (keys.hasNext()) { ja.put(keys.next()); } return ja.length() == 0 ? null : ja; }
/** * Get the string associated with an index. * * @param index * The index must be between 0 and length() - 1. * @return A string value. * @throws JSONException * If there is no value for the index. */ public String getString(int index) throws JSONException { return get(index).toString(); }
private JSONObject getLatestClassificationObjectOnBranch(String branchPath) throws RestClientException { final String classificationsUrl = urlHelper.getClassificationsUrl(branchPath); try { final JSONArray items = getItems(classificationsUrl); if (items != null && items.length() > 0) { return items.getJSONObject(items.length() - 1); } return null; } catch (Exception e) { throw new RestClientException("Failed to retrieve list of classifications.", e); } }
/** * Get the long value associated with an index. * * @param index * The index must be between 0 and length() - 1. * @return The value. * @throws JSONException * If the key is not found or if the value cannot be converted to a * number. */ public long getLong(int index) throws JSONException { Object o = get(index); return o instanceof Number ? ((Number) o).longValue() : (long) getDouble(index); }
/** * Produce a JSONArray containing the values of the members of this * JSONObject. * * @param names * A JSONArray containing a list of key strings. This determines the * sequence of the values in the result. * @return A JSONArray of values. * @throws JSONException * If any of the values are non-finite numbers. */ public JSONArray toJSONArray(JSONArray names) throws JSONException { if (names == null || names.length() == 0) { return null; } JSONArray ja = new JSONArray(); for (int i = 0; i < names.length(); i += 1) { ja.put(this.opt(names.getString(i))); } return ja; }
if (o instanceof JSONArray) { JSONArray array = JSONArray.class.cast(o); return array.get(index); } else { return null; for (int i = 0, len = array.length(); i < len; i++) { Object item = array.get(i); boolean test = at(0).test(item); if (test) { result = item; // evaluation can continue on this item break; if (item instanceof JSONObject) JSONObject obj = (JSONObject)item; if (obj.has(this.value.toString()))
/** * Get the optional object value associated with an index. * * @param index * The index must be between 0 and length() - 1. * @return An object value, or null if there is no object at that index. */ public Object opt(int index) { return (index < 0 || index >= length()) ? null : this.myArrayList.get(index); }
/** * 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; }
if (v instanceof JSONArray) { ja = (JSONArray)v; len = ja.length(); for (i = 0; i < len; i += 1) { if (i > 0) { b.append('\n'); b.append(escape(ja.get(i).toString())); len = ja.length(); for (i = 0; i < len; i += 1) { v = ja.get(i); if (v instanceof JSONArray) { b.append('<'); len = ja.length(); for (i = 0; i < len; ++i) { v = ja.opt(i); b.append(toString(v, (tagName == null) ? "array" : tagName));
public List<String> retrieveValidationStatuses(List<String> branchPaths) throws Exception { StringBuilder url = new StringBuilder(orchestrationUrl + VALIDATIONS_ENDPOINT + "/bulk/latest/statuses"); url.append("?paths="); boolean first = true; for (String branchPath : branchPaths) { if (!first) { url.append(","); } else { first = false; } url.append(branchPath); } final JSONResource resource = getResource(url.toString()); if (resource == null) { return null; } final JSONArray array = resource.array(); List<String> statuses = new ArrayList<>(); for (int a = 0; a < array.length(); a++) { final String string = array.getString(a); statuses.add(!string.equals("null") ? string : null); } return statuses; }
a = new JSONArray(s); System.out.println(a.toString()); System.out.println(""); System.out.println(a.toString(4)); System.out.println(JSONML.toString(a)); System.out.println(); System.out.println(a.toString(4)); System.out.println(JSONML.toString(a)); System.out.println(); System.out.println(new JSONArray(jj.toString()).toString(4)); JSONArray ja = new JSONArray(ar); System.out.println(ja.toString()); j.put("String", "98.6"); j.put("JSONObject", new JSONObject()); j.put("JSONArray", new JSONArray()); j.put("int", 57); j.put("double", 123456789012345678901234567890.); j.put("\\u2029", "\u2029"); a = j.getJSONArray("foo"); a.put(666); a.put(2001.99); a.put("so \"fine\"."); a.put("so <fine>.");
/** * Produce a comma delimited text from a JSONArray of JSONObjects using * a provided list of names. The list of names is not included in the * output. * @param names A JSONArray of strings. * @param ja A JSONArray of JSONObjects. * @return A comma delimited text. * @throws JSONException */ public static String toString(JSONArray names, JSONArray ja) throws JSONException { if (names == null || names.length() == 0) { return null; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < ja.length(); i += 1) { JSONObject jo = ja.optJSONObject(i); if (jo != null) { sb.append(rowToString(jo.toJSONArray(names))); } } return sb.toString(); } }
/** * Make a prettyprinted JSON text of this JSONArray. Warning: This method * assumes that the data structure is acyclical. * * @param indentFactor * The number of spaces to add to each level of indentation. * @return a printable, displayable, transmittable representation of the * object, beginning with <code>[</code> <small>(left * bracket)</small> and ending with <code>]</code> <small>(right * bracket)</small>. * @throws JSONException */ public String toString(int indentFactor) throws JSONException { return toString(indentFactor, 0); }