gpt4 book ai didi

android - 将 Arraylist 作为 json 对象的值发送?

转载 作者:行者123 更新时间:2023-11-29 14:38:29 25 4
gpt4 key购买 nike

我想将 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"}

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

最佳答案

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

关于android - 将 Arraylist 作为 json 对象的值发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21090937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com