使用GSON和Java解析另一个json对象中的嵌套json对象

时间:2020-06-23 16:40:11

标签: java json gson

我有以下json对象字符串:

{
    "_meta": {
        "success": true,
        "code": 200,
        "message": "OK. Everything worked as expected.",
        "totalCount": 2177,
        "pageCount": 109,
        "currentPage": 1,
        "perPage": 20,
        "rateLimit": {
            "limit": 30,
            "remaining": 29,
            "reset": 2
        }
    },
    "result": [
        {
            "id": "1830",
            "first_name": "Victor",
            "last_name": "Hugo",
            "gender": "male",
            "dob": "1986-12-09",
            "email": "hertha.hamill@example.net",
            "phone": "1-590-340-8979 x9953",
            "website": "https://www.green.com/distinctio-quo-beatae-facilis-dolorem-est",
            "address": "3988 Yasmeen Corner\nMertzville, NV 24572",
            "status": "active",
            "_links": {
                "self": {
                    "href": "https://gorest.co.in/public-api/users/1830"
                },
                "edit": {
                    "href": "https://gorest.co.in/public-api/users/1830"
                },
                "avatar": {
                    "href": "https://lorempixel.com/250/250/people/?49938"
                }
            }
        }
        
      ]
}

我现在想使用GSON来解析它,因为我现在使用此类:

public class GsonClass {
    boolean success;
    String message;
    int code, totalCount, currentPage, pageCount;

    public boolean isSuccess() {
        return success;
    }

    public String getMessage() {
        return message;
    }

    public int getCode() {
        return code;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getPageCount() {
        return pageCount;
    }

    @Override
    public String toString() {
        return "GsonClass{" +
                "success=" + success +
                ", message='" + message + '\'' +
                ", code=" + code +
                ", totalCount=" + totalCount +
                ", currentPage=" + currentPage +
                ", pageCount=" + pageCount +
                '}';
    }

    public static class RateLimit {
        int limit, remaining, reset;

        public void setLimit(int limit) {
            this.limit = limit;
        }

        public void setRemaining(int remaining) {
            this.remaining = remaining;
        }

        public void setReset(int reset) {
            this.reset = reset;
        }

        public int getLimit() {
            return limit;
        }

        public int getRemaining() {
            return remaining;
        }

        public int getReset() {
            return reset;
        }

        @Override
        public String toString() {
            return "RateLimit{" +
                    "limit=" + limit +
                    ", remaining=" + remaining +
                    ", reset=" + reset +
                    '}';
        }
    }

    public static class Result{
        String id,  user_id, title, gender, first_name, last_name, dob, email, phone, website,address, status

    }
}

我已将所有相关字段添加到类中,但是现在我试图序列化嵌套对象,因此我坚持使用它。 我不知道如何表示结果json数组字段中_links下的嵌套对象,以及如何表示self,edit和avatar,我是否必须表示一个类中的每个对象?

0 个答案:

没有答案