作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想使用 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/
我是一名优秀的程序员,十分优秀!