Java 如何构建要在HttpPost中使用的JSONobject请求

Java 如何构建要在HttpPost中使用的JSONobject请求,java,json,rest,http-post,Java,Json,Rest,Http Post,我这里有一个我需要构建的示例: { "sendSmsRequest": { "to": "5511982694404", "msg": "funcionou" } } 为此,我使用了JSONobject: JSONObject jsonObject = new JSONObject(); jsonObject.put("to", "123456789"); jsonObject.put("msg", "Mensagem Teste"); StringEntity inpu

我这里有一个我需要构建的示例:

{
  "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);
但我不知道如何把“标题”-“发送请求”。。。 有没有办法不用字符串就能做到这一点

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

谢谢你,Moondaisy,这正是我需要的!!