gpt4 book ai didi

android - 如何在 android 中使用 Volley 在 Json 对象中发布 Json 数组

转载 作者:太空狗 更新时间:2023-10-29 16:29:13 25 4
gpt4 key购买 nike

我想使用 POST METHOD 将这个 json 正文发送到服务器

{"device_type": "SUP","commands": [

{

"command_id": 165,

"arguments": [

{

"name": "Host",

"value": "google.com"

}

]

}]}

我尝试了很多网络上可用的解决方案,但大多数都告诉我格式化字符串并发送到服务器。有没有什么正确的方法可以使用截击将这个主体发送到服务器端。请帮我。谢谢。

最佳答案

让我们从底部开始。首先,

JSONObject json1= new JSONObject();
json1.put("name","Host");
json1.put("value","google.com");

之后我们将上面的对象放在一个数组中

JSONArray jsonArray = new JSONArray();
jsonArray.put(json1);

现在我们将上面的数组和命令id添加到一个新对象

JSONObject json2= new JSONObject();
json2.put("command_id","165");
json2.put("arguments",jsonArray);

同样这是命令数组的对象

JSONArray jsonArray2 = new JSONArray();
jsonArray2.put(json2);

现在我们把上面的数组和设备类型放到一个最终对象中

JSONObject json3= new JSONObject();
json3.put("device_type","SUP");
json3.put("commands",jsonArray2);

现在您可以将 json3 对象转换为字符串并发送到服务器。

json3.toString;

关于android - 如何在 android 中使用 Volley 在 Json 对象中发布 Json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43438232/

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