这是我需要构建的示例:
{
"sendSmsRequest": {
"to": "5511982694404",
"msg": "funcionou"
}
}
为了做到这一点,我使用了 JSONobject:
JSONObject jsonObject = new JSONObject();
jsonObject.put("to", "123456789");
jsonObject.put("msg", "Mensagem Teste");
StringEntity input = new StringEntity(jsonObject.toString());
这是请求:
post.setHeader("Accept", "application/json");
post.setHeader("Content-Type", "application/json");
post.setEntity(input);
但我不知道如何放置“header” - “sendSmsRequest”... 有什么方法可以在不使用字符串的情况下做到这一点?
String teste = "{\"sendSmsRequest\": { \"to\": \"123456789\",\"msg\": \"funcionou\"}}";
最佳答案
你可以这样做:
JSONObject jsonObject = new JSONObject();
jsonObject.put("to", "123456789");
jsonObject.put("msg", "Mensagem Teste");
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("sendSmsRequest", jsonObject);
StringEntity input = new StringEntity(jsonObject1.toString());
像这样,您可以在另一个 JSONObject
中拥有一个 JSONObject
。
关于java - 如何构建在 HttpPost 中使用的 JSONobject 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44976030/