简体   繁体   中英

how to get specific response json use javaWS in play framework

I access url with WSresponse and I have a response json like this

{
    "status": true,
    "message": "Success",
    "data": [
        {
            "name":"myname",
            "phone":"0888888888",
            "email":"email@example.com"
        }
    ]
}

is mycode to access the url

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,I hope it will help you..

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

thnks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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