使用 GSON 和 Java 正确解析 json

Parsing json correctly using GSON and Java

这是我的 json 字符串,因为我想将其转换为 Java Class:

"result": {
    "id": "39559",
    "first_name": "John",
    "last_name": "Who",
    "gender": "male",
    "dob": null,
    "email": "john.who@roberts.com",
    "phone": null,
    "website": null,
    "address": null,
    "status": "active",
    "_links": {
        "self": {
            "href": "https://gorest.co.in/public-api/users/39559"
        },
        "edit": {
            "href": "https://gorest.co.in/public-api/users/39559"
        },
        "avatar": {
            "href": null
        }
    }
}

这是我的 class(我删除了 getter 和 toString):

public class Result {
    String id, user_id, title, first_name, last_name, gender, dob, email, phone,
            website, address, status, _links, album_id, url, thumbnail, post_id, name, body;
}

我想知道我该如何代表

"_links": {
    "self": {
        "href": "https://gorest.co.in/public-api/users/39559"
    },
    "edit": {
        "href": "https://gorest.co.in/public-api/users/39559"
    },
    "avatar": {
        "href": null
    }
}

如何表示结果中缺失的部分 class?

_links 似乎是一个具有 selfeditavatar 属性的 JSON 对象。这些属性中的每一个也是 JSON 对象,都具有属性 href。 → 您需要这三个 类 来表示这些对象:

public class Result {
    String id, user_id, title, first_name, last_name, gender;
    String dob, email, phone, website, address, status;
    String album_id, url, thumbnail, post_id, name, body;
    Links _links;
}
public class Links {
    Link self, edit, avatar;
}
public class Link {
    String href;
}

您需要将链接创建为带有字段的对象