如何获得特定响应 json 在 play 框架中使用 javaWS
how to get specific response json use javaWS in play framework
我使用 WSresponse 访问 url,我得到了这样的响应 json
{
"status": true,
"message": "Success",
"data": [
{
"name":"myname",
"phone":"0888888888",
"email":"email@example.com"
}
]
}
是访问url
的mycode
CompletionStage<? extends WSResponse> res = ws.url("http://localhost:9000/check-user").get();
return res.thenApply(R->{
return ok(R.getBody(WSBodyReadables.instance.json()));
});
I expect to get specific data like this
{"name":"myname"}
如果您需要 JSON 响应的特定数据,在 java 中提供了一些 JSON 库,例如 **org.json.JSONObject** and org.json.JSONArray
,因此请下载 jar 并添加您的 java 投影那些库 jar 并尝试下面发布的代码,我希望它能帮助你..
JSONObject jsonObj = new JSONObject("jsonResponse");
JSONArray item = jsonObj.getJSONArray("data");
if (item.length() != 0) {
for (int i = 0; i < item.length(); i++) {
JSONObject json = item.getJSONObject(i);
String fromVersion = json.optString("name");
or
String fromVersion = json.getString("name");
}
}
感谢。
我使用 WSresponse 访问 url,我得到了这样的响应 json
{
"status": true,
"message": "Success",
"data": [
{
"name":"myname",
"phone":"0888888888",
"email":"email@example.com"
}
]
}
是访问url
的mycodeCompletionStage<? extends WSResponse> res = ws.url("http://localhost:9000/check-user").get();
return res.thenApply(R->{
return ok(R.getBody(WSBodyReadables.instance.json()));
});
I expect to get specific data like this
{"name":"myname"}
如果您需要 JSON 响应的特定数据,在 java 中提供了一些 JSON 库,例如 **org.json.JSONObject** and org.json.JSONArray
,因此请下载 jar 并添加您的 java 投影那些库 jar 并尝试下面发布的代码,我希望它能帮助你..
JSONObject jsonObj = new JSONObject("jsonResponse");
JSONArray item = jsonObj.getJSONArray("data");
if (item.length() != 0) {
for (int i = 0; i < item.length(); i++) {
JSONObject json = item.getJSONObject(i);
String fromVersion = json.optString("name");
or
String fromVersion = json.getString("name");
}
}
感谢。