2014-01-13 59 views
1

我想发送json对象作为post参数到php脚本。但我想发送它如下所示的这种格式发送Arraylist作为json对象的值?

{"people_ids":[118, 120],"userAdding":"123"} 

所以我可以将people_ids的值转换为php数组。

$peopleIDsArray = $json['people_ids']; 

但是,当我这样做,它打印json对象如下所示。

JSONObject jsonOBJ = new JSONObject(); 
     try { 
      jsonOBJ.put("userAdding", user._id); 
      jsonOBJ.put("people_ids", Arrays.toString(peopleIds.toArray())); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

// array as string。 (JSON对象)

{"person_ids":"[118, 120]","user_id":"123"} 

有没有什么办法,我可以实现上面的格式?

+0

凡'group_id'哪里来的?错误不在您发布的部分。 –

+0

感谢您的时间@Subir ..我已经更新了这个问题! 但Hariharan回复是我一直在寻找! – Taseen

回答

5

试试这个..如果peopleIdsArrayList的

JSONObject jsonOBJ = new JSONObject(); 
    try { 
     jsonOBJ.put("userAdding", user._id); 
     JSONArray list = new JSONArray(peopleIds); 
     jsonOBJ.put("people_ids", list); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
+0

谢谢..这就是我正在寻找的! – Taseen

+0

如何将这个json数组作为var发送给java脚本代码。我需要回顾一下,我的Java脚本代码中的json列表,如var people = list; (JSONArray类型)。 –