我正在尝试从我的 java 代码访问 API。API 只接受 JSON 对象形式的数据,但我在构建JsonObject
表示时遇到了麻烦。
我想要做的是使用createObjectBuilder()
创建JsonObject
格式如下:
{
"user1234@gmail.com": {
"username" : "user1234@gmail.com",
"password" : "password1",
"service" : [ "yourkit" ]
}
}
我试过这个:
JSONObject jsonObject = (JSONObject)Json.createObjectBuilder()
.add(
username,
Json.createObjectBuilder()
.add("username", username)
.add("password", password)
.add("service", service)
).build();
p.setEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
HttpResponse r = c.execute(p);
但它不会向 API 发送正确的请求。
有人可以帮帮我吗?