繁体   English   中英

如何解析asString()异常响应

[英]How to parse asString() unirest response

我有一个需要解析的unirest响应,它返回一个字符串响应。 请检查以下代码:

HttpResponse<String> response = Unirest.post("http://api.nuvelco.com/token")
                     .header("content-type", "application/x-www-form-urlencoded")
                     .header("cache-control", "no-cache")
                     .body("grant_type=password&username=" + uname + "&password=" + pword + "&client_id=paymentApp")
                     .asString();

我不知道如何解析asString()请求,因此将不胜感激。

编辑:我忘了包括响应

{
    "access_token":"HzDzAtlom6CDqRa0zPetH09hZbDr8tm__hPw7aCx2m0h0gnGwHMaKvBEp64sHRUCJJEAlhCNUqQ3tBSyvod_93gTnt145W2ly9KKw5ISmaZRN75O9NUfJUGPRd0LH87LlxiRgHNFkUGTUDwyJOmhYNajj7TQoncxqkfc3jxL-jEi3Ea1cGRvOSmLH5Aqom81kKmiRzPV_Ss0xwFWjQVsS03y_P720Hv1BQEayO9L7Vic4A64GmXm3PlFQuwcvOk3M_7WOa_EEGOFBZdhwn7dzNQ7gypJ27MSTOD3gI57880unF4XFgTT_H4p4G5V6C8L8yRbRNXPIe80gLKYk3F3nw",
    "token_type":"bearer",
    "expires_in":3599,
    "refresh_token":"f87a5fea7d764826be24bd742626d0d8",
    "as:client_id":"paymentApp",
    "username":"savemore01",
    ".issued":"Wed, 05 Dec 2018 03:13:23 GMT",
    ".expires":"Wed, 05 Dec 2018 04:13:23 GMT"
}

您可以将json-simple使用一个简单的Java库用于JSON。 因此,您可以将字符串响应值解析为类似这样的内容:

HttpResponse<String> httpResponse = Unirest.post("http://api.nuvelco.com/token")
                     .header("content-type", "application/x-www-form-urlencoded")
                     .header("cache-control", "no-cache")
                     .body("grant_type=password&username=" + uname + "&password=" + pword + "&client_id=paymentApp")
                     .asString();

String response = EntityUtils.toString(httpResponse.getEntity());

Object object = JSONValue.parse(response);

请注意,EntityUtils的程序包是org.apache.http.util

暂无
暂无

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

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