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

sum*_*hra 2 android json android-volley

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

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

{

  "command_id": 165,

  "arguments": [

    {

      "name": "Host",

      "value": "google.com"

    }

  ]

}]}
Run Code Online (Sandbox Code Playgroud)

我在网上尝试了很多可用的解决方案,但大多数都告诉我们格式化字符串并发送到服务器.有没有正确的方法使用齐射将此正文发送到服务器端.请帮我.谢谢.

Nir*_*yer 5

让我们从底部开始.第一,

JSONObject json1= new JSONObject();
json1.put("name","Host");
json1.put("value","google.com");
Run Code Online (Sandbox Code Playgroud)

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

JSONArray jsonArray = new JSONArray();
jsonArray.put(json1);
Run Code Online (Sandbox Code Playgroud)

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

JSONObject json2= new JSONObject();
json2.put("command_id","165");
json2.put("arguments",jsonArray);
Run Code Online (Sandbox Code Playgroud)

同样,这是命令数组的对象

JSONArray jsonArray2 = new JSONArray();
jsonArray2.put(json2);
Run Code Online (Sandbox Code Playgroud)

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

JSONObject json3= new JSONObject();
json3.put("device_type","SUP");
json3.put("commands",jsonArray2);
Run Code Online (Sandbox Code Playgroud)

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

json3.toString;
Run Code Online (Sandbox Code Playgroud)

  • 很棒的解释@Nirup Iyer感谢您的时间:) (2认同)