/// <summary> /// Produce a JSONArray containing the names of the elements of this JSONObject /// </summary> /// <returns>A JSONArray containing the key strings, or null if the JSONObject</returns> public JSONArray names() { JSONArray ja = new JSONArray(); //NOTE!! I choose to use keys stored in the ArrayList, to maintain sequence order foreach (string key in myKeyIndexList) { ja.put(key); } if (ja.Length() == 0) { return null; } return ja; }
/// <summary> /// Append an array of JSONObjects to current object /// </summary> /// <param name="names"></param> /// <returns></returns> public JSONArray toJSONArray(JSONArray names) { if (names == null | names.Length() == 0) return null; JSONArray ja = new JSONArray(); for (int i = 0; i < names.Length(); i++) { ja.put(this.opt(names.getString(i))); } return ja; }
/// <summary> /// Accumulate values under a key. It is similar to the put method except /// that if there is already an object stored under the key then a /// JSONArray is stored under the key to hold all of the accumulated values. /// If there is already a JSONArray, then the new value is appended to it. /// In contrast, the put method replaces the previous value. /// </summary> /// <param name="key">A key string.</param> /// <param name="val">An object to be accumulated under the key.</param> /// <returns>this</returns> public JSONObject accumulate(string key, object val) { JSONArray a; object obj = opt(key); if (obj == null) { put(key, val); } else if (obj is JSONArray) { a = (JSONArray)obj; a.put(val); } else { a = new JSONArray(); a.put(obj); a.put(val); put(key, a); } return this; }