2017-02-18 64 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);