-1

我正在尝试从我的 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 发送正确的请求。

有人可以帮帮我吗?

4

2 回答 2

2

要实现您希望变量服务的格式,必须是字符串数组。

于 2013-12-11T14:16:29.740 回答
1

您缺少服务属性的数组:

JSONObject jsonObject = (JSONObject)Json.createObjectBuilder()
                        .add(
                            username,
                            Json.createObjectBuilder()
                                .add("username", username)
                                .add("password", password)
                                .add("service",
                                    Json.createArrayBuilder()
                                      .add(service)
                                )
                        ).build();
于 2013-12-11T14:17:34.447 回答