Jay*_*nan 4 json playframework playframework-2.0
实际上我正在从一个播放应用程序重定向到另一个播放应用程序,最后我收到作为结果对象的响应..下面是两个应用程序中的操作。我正在从应用程序 1 重定向到应用程序 2。应用程序 2 将返回我需要提取的 JSON 字符串。
如何从 Result 对象中检索 JSON 内容?
应用1:
public static Result redirectTest(){
Result result = redirect("http://ipaddress:9000/authenticate");
/*** here I would like to extract JSON string from result***/
return result;
}
Run Code Online (Sandbox Code Playgroud)
应用2:
@SecuredAction
public static Result index() {
Map<String, String> response = new HashMap<String, String>();
DemoUser user = (DemoUser) ctx().args.get(SecureSocial.USER_KEY);
for(BasicProfile basicProfile: user.identities){
response.put("name", basicProfile.firstName().get());
response.put("emailId", basicProfile.email().get());
response.put("providerId", basicProfile.providerId());
response.put("avatarurl", basicProfile.avatarUrl().get());
}
return ok(new JSONObject(response).toString());
}
Run Code Online (Sandbox Code Playgroud)
小智 7
我想play.test.Helpers.contentAsString这就是你要找的。
Run Code Online (Sandbox Code Playgroud)public static java.lang.String contentAsString(Result result)将内容提取为字符串。
Play 2.8.x中仍然可用。
使用 JavaResultExtractor,例如:
Result result = ...;
byte[] body = JavaResultExtractor.getBody(result, 0L);
Run Code Online (Sandbox Code Playgroud)
拥有一个字节数组,您可以从 Content-Type 标头中提取字符集并创建 java.lang.String:
String header = JavaResultExtractor.getHeaders(result).get("Content-Type");
String charset = "utf-8";
if(header != null && header.contains("; charset=")){
charset = header.substring(header.indexOf("; charset=") + 10, header.length()).trim();
}
String bodyStr = new String(body, charset);
JsonNode bodyJson = Json.parse(bodyStr);
Run Code Online (Sandbox Code Playgroud)
上面的一些代码是从play.test.Helpers复制的
| 归档时间: |
|
| 查看次数: |
12245 次 |
| 最近记录: |