2016-08-06 94 views
-1
JSONObject jsonObj = new JSONObject("string"); 
jsonObj.toString(); 

"employees":[{"firstname":"stack","lastname":"overflow"}, {"firstname":"Happy","lastname":"Coding"}] 

我如何可以追加:追加JSON數組的JSONObject

{"firstname":"Gloomy","lastname":"Sunday"} 

以上JSON對象

我想

"employees":[{"firstname":"stack","lastname":"overflow"}, {"firstname":"Happy","lastname":"Coding"}, {"firstname":"Gloomy","lastname":"Sunday"}] 

upon jsonObj.toString(); 
+0

你肯定拳頭代碼示例產生於它的結束寫入字符串?數據從哪裏來? –

回答

0

請做研究這自報家門,這網站不是在這裏爲你做你的計算機科學作業。只是因爲我需要一些代表...

public class ex{ 
    void example(){ 
     try{ 
      JSONObject jobj = new JSONObject("[{firstname:stack,lastname:overflow}]"); 
      JSONObject jobj2 = new JSONObject("[{firstname:Happy,lastname:Coding}]"); 
      JSONArray jarray = new JSONArray(); 
      jarray.put(jobj); 
      jarray.put(jobj2); 
     }catch(JSONException e){ 
      e.printStackTrace(); 
     } 
    } 
} 
+0

搭售追加到現有的僱員對象,而不是創建新的對象。 – agilematrix

0
JSONObject jsonObj = new JSONObject(); 
JSONObject jo1 = new JSONObject("{"firstname":"Gloomy","lastname":"Sunday"}"); 
jsonObj.append("employees", jo1);