2017-02-18 102 views
1

爲什麼當我添加一個組件的列表不是「[]」?不完整的文件Json

try { 
      json.accumulate("name_channel", txt_channel.getText().toString()); 
      json.accumulate("channel_date", new Date().toString()); 
      JSONArray jsonArray = new JSONArray(); 
      JSONObject idJSON = new JSONObject(); 
      idJSON.accumulate("id",MainActivity.id_user); 
      json.accumulate("list_users", idJSON); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

它生成以下內容:

{"name_channel":"xxx","channel_date":"Sat Feb 18 12:00:27 GMT+01:00 2017","list_users":{"id":"1445353654356"}} 

但我需要:用[]

{"name_channel":"xxx","channel_date":"Sat Feb 18 12:00:27 GMT+01:00 2017","list_users":[{"id":"1445353654356"}]} 

回答

3

要做到這一點,你將需要做這樣的事情

JSONObject idJSON = new JSONObject(); 
idJSON.accumulate("id",MainActivity.id_user); 
JSONArray arr = new JSONArray(); 
arr.put(idJSON); 
json.accumulate("list_users", arr);