将unirest字符串响应放入哈希图中

时间:2018-12-10 08:34:14

标签: java unirest

我有一个来自我提出的unirest请求的字符串响应,我想知道是否可以将结果放入HashMap中以便更轻松地访问响应字段。任何建议都会有所帮助。

以下是回复示例:

{
    "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"
}

我试图通过使用逗号(,)作为分隔符来使用split,但是日期和时间字段搞砸了。我希望有人能帮忙。

2 个答案:

答案 0 :(得分:1)

根据您使用的技术,Unirest提供开箱即用的解析功能。 在下面可以找到以下示例:http://unirest.io/java.html

// Response to Object
HttpResponse<Book> bookResponse = 
Unirest.get("http://httpbin.org/books/1").asObject(Book.class);
Book bookObject = bookResponse.getBody();

答案 1 :(得分:1)

ObjectMapper是在对象和字符串之间转换的非常方便的助手。

1)将json字符串解析为哈希图:

    @Test
public void it_should_test() throws IOException {
    //Given
    String str = "{\n" +
            "    \"access_token\":\"HzDzAtlom6CDqRa0zPetH09hZbDr8tm__hPw7aCx2m0h0gnGwHMaKvBEp64sHRUCJJEAlhCNUqQ3tBSyvod_93gTnt145W2ly9KKw5ISmaZRN75O9NUfJUGPRd0LH87LlxiRgHNFkUGTUDwyJOmhYNajj7TQoncxqkfc3jxL-jEi3Ea1cGRvOSmLH5Aqom81kKmiRzPV_Ss0xwFWjQVsS03y_P720Hv1BQEayO9L7Vic4A64GmXm3PlFQuwcvOk3M_7WOa_EEGOFBZdhwn7dzNQ7gypJ27MSTOD3gI57880unF4XFgTT_H4p4G5V6C8L8yRbRNXPIe80gLKYk3F3nw\",\n" +
            "    \"token_type\":\"bearer\",\n" +
            "    \"expires_in\":3599,\n" +
            "    \"refresh_token\":\"f87a5fea7d764826be24bd742626d0d8\",\n" +
            "    \"as:client_id\":\"paymentApp\",\n" +
            "    \"username\":\"savemore01\",\n" +
            "    \".issued\":\"Wed, 05 Dec 2018 03:13:23 GMT\",\n" +
            "    \".expires\":\"Wed, 05 Dec 2018 04:13:23 GMT\"\n" +
            "}";

    //When
    Map<String, Object> response = new ObjectMapper().readValue(str, HashMap.class);

    //Then
    assertThat(response.get("access_token")).isEqualTo("HzDzAtlom6CDqRa0zPetH09hZbDr8tm__hPw7aCx2m0h0gnGwHMaKvBEp64sHRUCJJEAlhCNUqQ3tBSyvod_93gTnt145W2ly9KKw5ISmaZRN75O9NUfJUGPRd0LH87LlxiRgHNFkUGTUDwyJOmhYNajj7TQoncxqkfc3jxL-jEi3Ea1cGRvOSmLH5Aqom81kKmiRzPV_Ss0xwFWjQVsS03y_P720Hv1BQEayO9L7Vic4A64GmXm3PlFQuwcvOk3M_7WOa_EEGOFBZdhwn7dzNQ7gypJ27MSTOD3gI57880unF4XFgTT_H4p4G5V6C8L8yRbRNXPIe80gLKYk3F3nw");
    assertThat(response.get("expires_in")).isEqualTo(3599);
    assertThat(response.get("token_type")).isEqualTo("bearer");
    assertThat(response.get("refresh_token")).isEqualTo("f87a5fea7d764826be24bd742626d0d8");
    assertThat(response.get("as:client_id")).isEqualTo("paymentApp");
    assertThat(response.get(".issued")).isEqualTo("Wed, 05 Dec 2018 03:13:23 GMT");
    assertThat(response.get(".expires")).isEqualTo("Wed, 05 Dec 2018 04:13:23 GMT");
}

2)将json字符串解析为对象:

a)接收器类:

public class Response {

    @JsonProperty("access_token")
    private String accessToken;
    @JsonProperty("token_type")
    private String tokenType;
    @JsonProperty("expires_in")
    private Long expiresIn;
    @JsonProperty("refresh_token")
    private String refreshToken;
    @JsonProperty("as:client_id")
    private String clientId;
    @JsonProperty("username")
    private String username;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "EEE, dd MMM yyyy HH:mm:ss zzz")
    @JsonProperty(".issued")
    private Date issued;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "EEE, dd MMM yyyy HH:mm:ss zzz")
    @JsonProperty(".expires")
    private Date expires;

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getTokenType() {
        return tokenType;
    }

    public void setTokenType(String tokenType) {
        this.tokenType = tokenType;
    }

    public Long getExpiresIn() {
        return expiresIn;
    }

    public void setExpiresIn(Long expiresIn) {
        this.expiresIn = expiresIn;
    }

    public String getRefreshToken() {
        return refreshToken;
    }

    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getIssued() {
        return issued;
    }

    public void setIssued(Date issued) {
        this.issued = issued;
    }

    public Date getExpires() {
        return expires;
    }

    public void setExpires(Date expires) {
        this.expires = expires;
    }
}

b)解析:

    @Test
public void it_should_test_2() throws IOException, ParseException {
    //Given
    String str = "{\n" +
            "    \"access_token\":\"HzDzAtlom6CDqRa0zPetH09hZbDr8tm__hPw7aCx2m0h0gnGwHMaKvBEp64sHRUCJJEAlhCNUqQ3tBSyvod_93gTnt145W2ly9KKw5ISmaZRN75O9NUfJUGPRd0LH87LlxiRgHNFkUGTUDwyJOmhYNajj7TQoncxqkfc3jxL-jEi3Ea1cGRvOSmLH5Aqom81kKmiRzPV_Ss0xwFWjQVsS03y_P720Hv1BQEayO9L7Vic4A64GmXm3PlFQuwcvOk3M_7WOa_EEGOFBZdhwn7dzNQ7gypJ27MSTOD3gI57880unF4XFgTT_H4p4G5V6C8L8yRbRNXPIe80gLKYk3F3nw\",\n" +
            "    \"token_type\":\"bearer\",\n" +
            "    \"expires_in\":3599,\n" +
            "    \"refresh_token\":\"f87a5fea7d764826be24bd742626d0d8\",\n" +
            "    \"as:client_id\":\"paymentApp\",\n" +
            "    \"username\":\"savemore01\",\n" +
            "    \".issued\":\"Wed, 05 Dec 2018 03:13:23 GMT\",\n" +
            "    \".expires\":\"Wed, 05 Dec 2018 04:13:23 GMT\"\n" +
            "}";

    //When
    Response response = new ObjectMapper().readValue(str, Response.class);

    //Then
    assertThat(response.getAccessToken()).isEqualTo("HzDzAtlom6CDqRa0zPetH09hZbDr8tm__hPw7aCx2m0h0gnGwHMaKvBEp64sHRUCJJEAlhCNUqQ3tBSyvod_93gTnt145W2ly9KKw5ISmaZRN75O9NUfJUGPRd0LH87LlxiRgHNFkUGTUDwyJOmhYNajj7TQoncxqkfc3jxL-jEi3Ea1cGRvOSmLH5Aqom81kKmiRzPV_Ss0xwFWjQVsS03y_P720Hv1BQEayO9L7Vic4A64GmXm3PlFQuwcvOk3M_7WOa_EEGOFBZdhwn7dzNQ7gypJ27MSTOD3gI57880unF4XFgTT_H4p4G5V6C8L8yRbRNXPIe80gLKYk3F3nw");
    assertThat(response.getExpiresIn()).isEqualTo(3599L);
    assertThat(response.getTokenType()).isEqualTo("bearer");
    assertThat(response.getRefreshToken()).isEqualTo("f87a5fea7d764826be24bd742626d0d8");
    assertThat(response.getClientId()).isEqualTo("paymentApp");
    assertThat(response.getIssued()).isEqualTo(DateUtils.parseDate("Wed, 05 Dec 2018 03:13:23 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz"));
    assertThat(response.getExpires()).isEqualTo(DateUtils.parseDate("Wed, 05 Dec 2018 04:13:23 GMT", "EEE, dd MMM yyyy HH:mm:ss zzz"));
}

请注意,我们可以通过JsonFormat批注轻松地将具有任何所需模式的日期值应用于Date字段。