JSONObject累加和put之间有什么区别?
我已经涉猎了JSON,我在文档(JAVA)中看到,JSONObject的put()和acculate()几乎做了相同的事情JSONObject累加和put之间有什么区别?,json,jsonobject,Json,Jsonobject,我已经涉猎了JSON,我在文档(JAVA)中看到,JSONObject的put()和acculate()几乎做了相同的事情 这是关于什么的?我看到了JSONObject的Java源代码,accumulate和put之间的区别在于,使用accumulate(字符串键,对象值),如果“键”存在一些值,则检查对象是否为数组,如果是数组,则检查“值”将添加到数组中,否则将为此键创建一个数组 但是,在put中,如果键存在,它的值将替换为值-“value” 这是JSONObject累加的来源(字符串键、对象
这是关于什么的?我看到了JSONObject的Java源代码,accumulate和put之间的区别在于,使用accumulate(字符串键,对象值),如果“键”存在一些值,则检查对象是否为数组,如果是数组,则检查“值”将添加到数组中,否则将为此键创建一个数组 但是,在put中,如果键存在,它的值将替换为值-“value” 这是JSONObject累加的来源(字符串键、对象值)
你用的是哪一个库?android一个,有什么不同吗?所以,如果我需要创建一个简单的一级json对象,它在性能方面更好,是累积还是放置?@HamzehSoboh我认为放置会更好,但除非你的对象很大,否则我认为不会有很大的性能差异。
/**
* Appends {@code value} to the array already mapped to {@code name}. If
* this object has no mapping for {@code name}, this inserts a new mapping.
* If the mapping exists but its value is not an array, the existing
* and new values are inserted in order into a new array which is itself
* mapped to {@code name}. In aggregate, this allows values to be added to a
* mapping one at a time.
*
* <p> Note that {@code append(String, Object)} provides better semantics.
* In particular, the mapping for {@code name} will <b>always</b> be a
* {@link JSONArray}. Using {@code accumulate} will result in either a
* {@link JSONArray} or a mapping whose type is the type of {@code value}
* depending on the number of calls to it.
*
* @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
* Integer, Long, Double, {@link #NULL} or null. May not be {@link
* Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}.
*/
public JSONObject accumulate(String name, Object value) throws JSONException {
Object current = nameValuePairs.get(checkName(name));
if (current == null) {
return put(name, value);
}
if (current instanceof JSONArray) {
JSONArray array = (JSONArray) current;
array.checkedPut(value);
} else {
JSONArray array = new JSONArray();
array.checkedPut(current);
array.checkedPut(value);
nameValuePairs.put(name, array);
}
return this;
}
/**
* Maps {@code name} to {@code value}, clobbering any existing name/value
* mapping with the same name.
*
* @return this object.
*/
public JSONObject put(String name, boolean value) throws JSONException {
nameValuePairs.put(checkName(name), value);
return this;
}