这是我的 Java 程序,它试图将 JSON 对象发送到服务器。我将 Apache HTTPClient 用于 HTTP 请求,并将 Jettison 用作 JSON 库。
我对此没有什么疑问。
POST 数组中 JSON 字符串的结构是什么。就像是
{xxxxx:{userId:userId, blha, blha, blha.........}}
如果我只需要从服务器端的 POST 数组中获取 JSON 字符串(无需转换为对象)。怎么做?在php中我们这样做
echo $_POST["xxxxxxx"];
通常 POST 数组中的每个数据都有一个名称。但下面的程序没有为 JSON 对象指定任何名称。POST 数组中以下 JSON 字符串的名称(xxxxxxx)是什么。
string base_url = "https://abc.com/";
string username = "test_user";
string password = "test_user_pw";
string client_id = "test_user123";
string client_secret = "test_user1234567";
string login_url = base_url + "session/login";
CloseableHttpClient wf_client = HttpClients.custom().setUserAgent(client_id + "/1.0").build();
HttpPost login_post = new HttpPost(loginUrl);
JSONObject login_object = new JSONObject();
try {
login_object.put("userId", username);
login_object.put("password", password);
login_object.put("clientId", client_id);
login_object.put("clientSecret", client_secret);
} catch (JSONException ex) {
System.out.println(ex.toString());
}
StringEntity post_entity = new StringEntity(login_object.toString(), jason_content_type);
login_post.setEntity(post_entity);
CloseableHttpResponse responce = wf_client.execute(login_post);