2015-03-05 3 views
6

मैं जेएसओएन के साथ डबलिंग कर रहा हूं और मैं देखता हूं कि प्रलेखन (जावा) में, JSONObject's put() और जमा() बहुत कुछ वही करता है?JSONObject जमा करने और रखे जाने के बीच क्या अंतर है?

इसके बारे में क्या है?

+0

कौन सा पुस्तकालय आप उपयोग कर रहे के लिए कोड है? –

+0

एंड्रॉइड एक, क्या इससे कोई फर्क पड़ता है? –

उत्तर

14

मैंने JSONObject के लिए जावा स्रोत कोड देखा और संचय और रखरखाव के बीच का अंतर यह है कि "कुंजी" के लिए कुछ मान मौजूद है, तो ऑब्जेक्ट को सरणी के लिए चेक किया गया है, यदि यह एक सरणी है तो "मान" सरणी में जोड़ा जाता है अन्यथा इस कुंजी के लिए एक सरणी बनाई जाती है। "मूल्य"

यहाँ JSONObject के स्रोत जमा (स्ट्रिंग कुंजी, वस्तु मूल्य)

/** 
* 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; 
} 
है -

पुट में, हालांकि, कुंजी यदि वह मौजूद है, यह मूल्य मान से बदल रहा है

और यहाँ JSONObject पुट (स्ट्रिंग कुंजी, वस्तु मूल्य)

/** 
* 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; 
} 
+0

तो, यदि मुझे एक साधारण-स्तर-जेसन ऑब्जेक्ट बनाने की आवश्यकता है, जो प्रदर्शन के संदर्भ में बेहतर है, जमा करें या डालें? –

+0

@HamzehSoboh मुझे लगता है कि डाल बेहतर होगा, लेकिन जब तक कि आपका ऑब्जेक्ट बड़ा न हो, मुझे नहीं लगता कि वहां एक बड़ा प्रदर्शन अंतर होगा। –

+0

ग्रेट उत्तर। धन्यवाद! –

संबंधित मुद्दे