2013-10-24 8 views
13

Qual è il modo migliore per combinare (unire) due JSONObjects?Qual è il modo migliore di combinare (unire) 2 oggetti JSONObjects?

JSONObject o1 = { 
    "one": "1", 
    "two": "2", 
    "three": "3" 
} 
JSONObject o2 = { 
     "four": "4", 
     "five": "5", 
     "six": "6" 
    } 

E risultato della combinazione o1 e o2 deve essere

JSONObject result = { 
     "one": "1", 
     "two": "2", 
     "three": "3", 
     "four": "4", 
     "five": "5", 
     "six": "6" 
    } 
+0

A quale lingua si riferisce? –

+0

E 'Android/Java. – Akshay

+0

Cosa vuoi che faccia se entrambi gli oggetti contengono la stessa chiave? –

risposta

3

oggetti json essere fondono in quel nuovo oggetto JSON simili.

JSONObject jObj = new JSONObject(); 
    jObj.put("one", "1"); 
    jObj.put("two", "2"); 
    JSONObject jObj2 = new JSONObject(); 
    jObj2.put("three", "3"); 
    jObj2.put("four", "4"); 


    JSONParser p = new JSONParser(); 
    net.minidev.json.JSONObject o1 = (net.minidev.json.JSONObject) p 
         .parse(jObj.toString()); 
    net.minidev.json.JSONObject o2 = (net.minidev.json.JSONObject) p 
         .parse(jObj2.toString()); 

    o1.merge(o2); 

    Log.print(o1.toJSONString()); 

ora o1 sarà l'oggetto json unito. si otterrà l'output come questo ::

{"three":"3","two":"2","four":"4","one":"1"} 

consultare questo link e scaricare la libreria smartjson ..here è il legame http://code.google.com/p/json-smart/wiki/MergeSample

spero che sarà di aiuto.

+0

Non credo che farlo in questo modo darebbe l'output richiesto nell'OP –

+0

@Neurenor - in questo modo forniremo l'output sbagliato.Vedere la domanda – jimpanzer

+0

per favore controlla la risposta modificata –

-1

Prova questo .. speriamo vi sia utile

JSONObject result = new JSONObject(); 
result.putAll(o1); 
result.putAll(O2); 
+1

Siamo spiacenti, ma non riesco a trovare il metodo "putAll" in org.json.JSONObject [Source] (http://developer.android.com/reference/org/json/JSONObject.html) – jimpanzer

+0

Il metodo putAll funziona solo in JAVA non in Android. –

+0

putAll sostituirà tutti i mapping correnti che si trovano nel risultato jsonobject, ovvero quando l'oggetto o1 viene inserito e quindi viene inserito anche l'oggetto o2, o1 verrà sostituito e solo o2 rimarrà nell'oggetto risultato –

9

devo tuo stesso problema: non riesco a trovare il metodo putAll (e non è elencato nella official reference page).

Quindi, non so se questa è la soluzione migliore, ma sicuramente funziona abbastanza bene:

//I assume that your two JSONObjects are o1 and o2 
JSONObject mergedObj = new JSONObject(); 

Iterator i1 = o1.keys(); 
Iterator i2 = o2.keys(); 
String tmp_key; 
while(i1.hasNext()) { 
    tmp_key = (String) i1.next(); 
    mergedObj.put(tmp_key, o1.get(tmp_key)); 
} 
while(i2.hasNext()) { 
    tmp_key = (String) i2.next(); 
    mergedObj.put(tmp_key, o2.get(tmp_key)); 
} 

Ora, il JSONObject fusa viene memorizzato in mergedObj

+0

Grazie per la risposta. Questo è lo stesso di @TomHart proposto nei commenti. – jimpanzer

0

Che ne dite di questo:

  Iterator iterator = json2.keys(); 
      while(iterator.hasNext()){ 
       String key = iterator.next().toString(); 
       json1.put(key,map.optJSONObject(key)); 
      } 
+0

Qual è il tipo di mappa variabile – Cliff

Problemi correlati