簡體   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