【问题标题】:Send Arraylist as value of json object?发送 Arraylist 作为 json 对象的值?
【发布时间】:2014-01-13 12:09:05
【问题描述】:

我想将 json 对象作为 post 参数发送到 php 脚本。但我想按如下所示的格式发送它

{"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();
        }

// 数组作为字符串。 (JSON 对象)

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

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

【问题讨论】:

  • group_id 来自哪里?错误不在您发布的部分。
  • 感谢您抽出宝贵时间@Subir .. 我已经更新了问题!!但 Hariharan 的回复正是我想要的!!

标签: android json arraylist


【解决方案1】:

试试这个.. 如果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();
    }

【讨论】:

  • 如何将此 json 数组作为 var 发送到 java 脚本代码。我需要在我的 java 脚本代码中检索该 json 列表,例如 var people=list; (JSONArray 类型)。
【解决方案2】:

将数组发送到 JSON 的非常简单的方法

 ArrayList<Model> arrayList = ArrayList()    
JSONObject jsonOBJ = new JSONObject();
        try {
            jsonOBJ.put("userAdding", arrayList.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

【讨论】:

    猜你喜欢
    • 2018-11-03
    • 2013-02-09
    • 2021-01-26
    • 1970-01-01
    • 2017-06-02
    • 2019-06-21
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多