簡體   English   中英

如何解析單個 json object 響應?

[英]How to parse a single json object responses?

我正在嘗試解析/轉換對 POJO 的 get 響應。 我有下面的用戶 class 和 Controller class。

在 getUsers() 方法中,我可以使用帶有包裝器 class 的ParameterizedTypeReference來解析請求。 但是,我無法解析“單個對象”響應。

對於 getUser 響應,如果我通過將ParameterizedTypeReference<UsersWrapper>()更改為ParameterizedTypeReference<User>()或使用 getUser 方法中顯示的代碼來使用相同的代碼,我會為我的用戶 class 獲得null值。

我假設我無法解析 JSON 因為{ "user": {.. wrapper.

如何使用以下方法從 JSON 獲取用戶 class 的值:

@GetMapping("/{id}")
public User getUser(@PathVariable long id) {}

JSON:

    {
    "user": {
        "id": 375607441100,
        "url": "https://example.com/users/375607441100.json",
        "name": "example-user",
        "email": "example-user@hotmail.com",
        "created_at": "2020-11-25T09:13:40Z",
        "updated_at": "2020-11-25T09:16:44Z",
        "time_zone": "Europe/Warsaw",
        "iana_time_zone": "Europe/Warsaw",
        "phone": null,
        "shared_phone_number": null,
        "photo": null,
        "locale_id": 1,
        "locale": "en-US",
        "organization_id": null,
        "role": "admin",
        "verified": true,
        "external_id": null,
        "tags": [],
        "alias": null,
        "active": true,
        "shared": false,
        "shared_agent": false,
        "last_login_at": "2020-11-25T09:16:35Z",
        "two_factor_auth_enabled": null,
        "signature": null,
        "details": null,
        "notes": null,
        "role_type": null,
        "custom_role_id": null,
        "moderator": true,
        "ticket_restriction": null,
        "only_private_comments": false,
        "restricted_agent": false,
        "suspended": false,
        "chat_only": false,
        "default_group_id": 360005501980,
        "report_csv": false,
        "user_fields": {}
    }
}

用戶 class:

@Data
public class User {
    private long id;
    private String name;
    private String email;
    private String role;
}

用戶包裝器:

@Data
@AllArgsConstructor
public class UsersWrapper {
    private List<User> users = new ArrayList<>();

    public UsersWrapper() {
    }
}

Controller getUsers():

@GetMapping
    public List<User> getUsers() {

        UsersWrapper claims = new UsersWrapper();

        try {
            ResponseEntity<UsersWrapper> claimResponse = restTemplate.exchange(
                    resourceUrl,
                    HttpMethod.GET,
                    request(), // headers with basicAuth -> username:token
                    new ParameterizedTypeReference<UsersWrapper>() {
                    });

            if (claimResponse != null && claimResponse.hasBody()) {
                claims = claimResponse.getBody();
            }
        } catch (
                RestClientException e) {
            e.printStackTrace();
        }

        List<User> users = claims.getUsers();

        return users;
}

獲取用戶():

@GetMapping("/{id}")
    public User getUser(@PathVariable long id) {

        String url = resourceUrl + "/" + id;
        ResponseEntity<User> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                request(), User.class);

        User user = response.getBody();
        return user;
}

使用@JsonProperty("user")創建以下包裝器 class 解決了這個問題。

@Data
@AllArgsConstructor
public class UserWrapper {
    private UserDTO user;

    public UserWrapper() {
    }

    @JsonProperty("user")
    public UserDTO getUser() {
        return user;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM