JSONObject put,accumulate,element的區別

public Object put (Object key, Object value) 將value映射到key下。若是此JSONObject對象以前存在一個value在這個key下,當前的value會替換掉以前的value
Associates the specified value with the specified key in this map(optional operation). If the map previously contained . a mapping for this key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true .))
 
public JSONObject accumulate (String key, Object value) 累積value到這個key下。這個方法同element()方法相似,特殊的是,若是當前已經存在一個value在這個key下那麼一個JSONArray將會存儲在這個key下來保存全部累積的value。若是已經存在一個JSONArray,那麼當前的value就會添加到這個JSONArray中
。相比之下replace方法會替代先前的value
Accumulate values under a key. It is similar to the element 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 replace method replaces the previous value.
 
public JSONObject element (String key, Object value) 將鍵/值對放到這個JSONObject對象裏面。若是當前value爲空(null),那麼若是這個key存在的話,這個key就會移除掉。若是這
個key以前有value值,那麼此方法會調用accumulate()方法。
Put a key/value pair in the JSONObject. If the value is null, then the key will be removed from the JSONObject if it is 
present. If there is a previous value assigned to the key, it will call accumulate.
相關文章
相關標籤/搜索