繁体   English   中英

如何获得特定响应 json 在播放框架中使用 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"}

If you need specific data with JSON Response,In java provides some JSON library like **org.json.JSONObject** and org.json.JSONArray , so Download the jar and add your java project those library jar's and try below posted code,我希望它会帮助你..

            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");  
                }
            } 

谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM