/** * Append an object value. This increases the array's length by one. <br> * 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)} * * @param value 值,可以是: Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONNull.NULL。 * @return this. */ public JSONArray put(Object value) { this.add(value); return this; }
/** * Append an object value. This increases the array's length by one. <br> * 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)} * * @param value 值,可以是: Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONNull.NULL。 * @return this. */ public JSONArray put(Object value) { this.add(value); return this; }
/** * 构造<br> * 将参数数组中的元素转换为JSON对应的对象加入到JSONArray中 * * @param list 初始化的JSON数组 */ public JSONArray(Iterable<Object> list) { this(); for (Object o : list) { this.add(o); } }
/** * 加入或者替换JSONArray中指定Index的值,如果index大于JSONArray的长度,将在指定index设置值,之前的位置填充JSONNull.Null * * @param index 位置 * @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL. * @return this. * @throws JSONException index < 0 或者非有限的数字 */ public JSONArray put(int index, Object value) throws JSONException { this.add(index, value); return this; }
/** * 构造<br> * 将参数数组中的元素转换为JSON对应的对象加入到JSONArray中 * * @param list 初始化的JSON数组 */ public JSONArray(Iterable<Object> list) { this(); for (Object o : list) { this.add(o); } }
/** * 加入或者替换JSONArray中指定Index的值,如果index大于JSONArray的长度,将在指定index设置值,之前的位置填充JSONNull.Null * * @param index 位置 * @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL. * @return this. * @throws JSONException index < 0 或者非有限的数字 */ public JSONArray put(int index, Object value) throws JSONException { this.add(index, value); return this; }
/** * 构造<br> * 将参数数组中的元素转换为JSON对应的对象加入到JSONArray中 * * @param list 初始化的JSON数组 */ public JSONArray(Collection<Object> list) { this(list.size()); for (Object o : list) { this.add(o); } }
/** * 构造<br> * 将参数数组中的元素转换为JSON对应的对象加入到JSONArray中 * * @param list 初始化的JSON数组 */ public JSONArray(Collection<Object> list) { this(list.size()); for (Object o : list) { this.add(o); } }
@Override public boolean addAll(Collection<? extends Object> c) { if (CollUtil.isEmpty(c)) { return false; } for (Object obj : c) { this.add(obj); } return true; }
@Override public boolean addAll(Collection<? extends Object> c) { if (CollUtil.isEmpty(c)) { return false; } for (Object obj : c) { this.add(obj); } return true; }
/** * 初始化 * * @param object 数组或集合或JSON数组字符串 * @throws JSONException 非数组或集合 */ private void init(Object object) throws JSONException{ if (object instanceof CharSequence) { // JSON字符串 init((CharSequence) object); } else { Iterator<?> iter; if (object.getClass().isArray()) {// 数组 iter = new ArrayIter<>(object); } else if (object instanceof Iterator<?>) {// Iterator iter = ((Iterator<?>) object); } else if (object instanceof Iterable<?>) {// Iterable iter = ((Iterable<?>) object).iterator(); } else { throw new JSONException("JSONArray initial value should be a string or collection or array."); } while (iter.hasNext()) { this.add(iter.next()); } } }
/** * 初始化 * * @param object 数组或集合或JSON数组字符串 * @throws JSONException 非数组或集合 */ private void init(Object object) throws JSONException{ if (object instanceof CharSequence) { // JSON字符串 init((CharSequence) object); } else { Iterator<?> iter; if (object.getClass().isArray()) {// 数组 iter = new ArrayIter<>(object); } else if (object instanceof Iterator<?>) {// Iterator iter = ((Iterator<?>) object); } else if (object instanceof Iterable<?>) {// Iterable iter = ((Iterable<?>) object).iterator(); } else { throw new JSONException("JSONArray initial value should be a string or collection or array."); } while (iter.hasNext()) { this.add(iter.next()); } } }
@Override public void add(int index, Object element) { if (index < 0) { throw new JSONException("JSONArray[" + index + "] not found."); } if (index < this.size()) { InternalJSONUtil.testValidity(element); this.rawList.set(index, JSONUtil.wrap(element, this.config.isIgnoreNullValue())); } else { while (index != this.size()) { this.add(JSONNull.NULL); } this.put(element); } }
@Override public void add(int index, Object element) { if (index < 0) { throw new JSONException("JSONArray[" + index + "] not found."); } if (index < this.size()) { InternalJSONUtil.testValidity(element); this.rawList.set(index, JSONUtil.wrap(element, this.config.isIgnoreNullValue())); } else { while (index != this.size()) { this.add(JSONNull.NULL); } this.put(element); } }
/** * 加入或者替换JSONArray中指定Index的值,如果index大于JSONArray的长度,将在指定index设置值,之前的位置填充JSONNull.Null * * @param index 位置 * @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL. * @return this. * @throws JSONException index < 0 或者非有限的数字 */ public JSONArray put(int index, Object value) throws JSONException { this.add(index, value); return this; }
/** * 构造<br> * 将参数数组中的元素转换为JSON对应的对象加入到JSONArray中 * * @param list 初始化的JSON数组 */ public JSONArray(Collection<Object> list) { this(list.size()); for (Object o : list) { this.add(o); } }
@Override public boolean addAll(Collection<? extends Object> c) { if (CollUtil.isEmpty(c)) { return false; } for (Object obj : c) { this.add(obj); } return true; }
public static JSONArray toJsonArray() { JSONArray array = new JSONArray(); for (UserStatusEnum e : values()) { JSONObject item = new JSONObject(); item.put("key", e.getKey()); item.put("name", e.getName()); array.add(item); } return array; } }