/** * Put a value in the JSONArray, where the value will be a * JSONArray which is produced from a Collection. * @param value A Collection value. * @return this. */ public JSONArray put(Collection value) { put(new JSONArray(value)); return this; }
private JSONArray getJSONArray(Object data) { // An empty string is considered like a null if (data==null || (data instanceof String && data.toString().equals(""))) return null; if(data instanceof JSONArray) { return (JSONArray)data; } else { // if it's not an array, make it a one element array return new JSONArray().put(data); } }
/** * Put a key/value pair in the JSONObject, where the value will be a * JSONArray which is produced from a Collection. * @param key A key string. * @param value A Collection value. * @return this. * @throws JSONException */ public JSONObject put(String key, Collection value) throws JSONException { put(key, new JSONArray(value)); return this; }
/** * Put a value in the JSONArray, where the value will be a * JSONArray which is produced from a Collection. * @param index The subscript. * @param value A Collection value. * @return this. * @throws JSONException If the index is negative or if the value is * not finite. */ public JSONArray put(int index, Collection value) throws JSONException { put(index, new JSONArray(value)); return this; }
/** * 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 keys = this.keys(); while (keys.hasNext()) { ja.put(keys.next()); } return ja.length() == 0 ? null : ja; }
jObj = new JSONObject(txt); } else if (txt.startsWith("[")){ jObj = new JSONArray(txt);
/** * Serializes a Hive List using a JSONArray * * @param obj the object to serialize * @param loi the object's inspector * @return */ private JSONArray serializeList(Object obj, ListObjectInspector loi) { // could be an array of whatever! // we do it in reverse order since the JSONArray is grown on demand, // as higher indexes are added. if(obj==null) { return null; } JSONArray ar = new JSONArray(); for(int i=loi.getListLength(obj)-1; i>=0; i--) { Object element = loi.getListElement(obj, i); try { ar.put(i, serializeField(element, loi.getListElementObjectInspector() ) ); } catch (JSONException ex) { LOG.warn("Problem serializing array", ex); throw new RuntimeException(ex); } } return ar; }
object = new JSONArray(object);
case '[': back(); return new JSONArray(this); default: break;
/** * Produce a JSONArray of JSONObjects from a comma delimited text string * using a supplied JSONArray as the source of element names. * @param names A JSONArray of strings. * @param x A JSONTokener of the source text. * @return A JSONArray of JSONObjects. * @throws JSONException */ public static JSONArray toJSONArray(JSONArray names, JSONTokener x) throws JSONException { if (names == null || names.length() == 0) { return null; } JSONArray ja = new JSONArray(); for (;;) { JSONObject jo = rowToJSONObject(names, x); if (jo == null) { break; } ja.put(jo); } if (ja.length() == 0) { return null; } return ja; }
/** * Append values to the array under a key. If the key does not exist in the * JSONObject, then the key is put in the JSONObject with its value being a * JSONArray containing the value parameter. If the key was already * associated with a JSONArray, then the value parameter is appended to it. * @param key A key string. * @param value An object to be accumulated under the key. * @return this. * @throws JSONException If the key is null or if the current value * associated with the key is not a JSONArray. */ public JSONObject append(String key, Object value) throws JSONException { testValidity(value); Object object = opt(key); if (object == null) { put(key.toLowerCase(), new JSONArray().put(value)); } else if (object instanceof JSONArray) { put(key.toLowerCase(), ((JSONArray)object).put(value)); } else { throw new JSONException("JSONObject[" + key + "] is not a JSONArray."); } return this; }
/** * 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; }
JSONArray ja = new JSONArray(); for (;;) { String value = getValue(x);
newja = new JSONArray(); newjo = new JSONObject(); if (arrayForm) {