如何使用Gson将JSON对象解析为自定义对象?

发布于 2024-11-06 08:54:57 字数 1183 浏览 0 评论 0原文

我有以下 JSON 对象作为字符串:

[{"Add1":"101","Description":null,"ID":1,"Name":"Bundesverfassung","Short":"BV"},{"Add1":"220","Description":null,"ID":2,"Name":"Obligationenrecht","Short":"OR"},{"Add1":"210","Description":null,"ID":3,"Name":"Schweizerisches Zivilgesetzbuch","Short":"ZGB"},{"Add1":"311_0","Description":null,"ID":4,"Name":"Schweizerisches Strafgesetzbuch","Short":null}]

现在我创建了一个代表结果之一的类:

public class Book {

    private int number;
    private String description;
    private int id;
    private String name;
    private String abbrevation;

    public Book(int number, String description, int id, String name, String abbrevation) {
        this.number = number;
        this.description = description;
        this.id = id;
        this.name = name;
        this.abbrevation = abbrevation;
    }

}

现在我想使用 Gson 将 JSON 对象解析为 Book 对象列表。我尝试了这种方法,但显然它不起作用。我该如何解决这个问题?

public static Book[] fromJSONtoBook(String response) {
        Gson gson = new Gson();
        return gson.fromJson(response, Book[].class);
    }

I have the following JSON object as a String:

[{"Add1":"101","Description":null,"ID":1,"Name":"Bundesverfassung","Short":"BV"},{"Add1":"220","Description":null,"ID":2,"Name":"Obligationenrecht","Short":"OR"},{"Add1":"210","Description":null,"ID":3,"Name":"Schweizerisches Zivilgesetzbuch","Short":"ZGB"},{"Add1":"311_0","Description":null,"ID":4,"Name":"Schweizerisches Strafgesetzbuch","Short":null}]

Now i created a class that represents one of the results:

public class Book {

    private int number;
    private String description;
    private int id;
    private String name;
    private String abbrevation;

    public Book(int number, String description, int id, String name, String abbrevation) {
        this.number = number;
        this.description = description;
        this.id = id;
        this.name = name;
        this.abbrevation = abbrevation;
    }

}

Now I want to use Gson to parse the JSON object into a list of Book objects. I tried it this way, but obviously it doesn't work. How can I fix that?

public static Book[] fromJSONtoBook(String response) {
        Gson gson = new Gson();
        return gson.fromJson(response, Book[].class);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゝ偶尔ゞ 2024-11-13 08:54:57

答案很简单,你必须使用注释 SerializedName 来指示 JSON 对象的哪一部分用于将 JSON 对象解析为 Book 对象:

public class Book {

    @SerializedName("Add1")
    private String number;

    @SerializedName("Description")
    private String description;

    @SerializedName("ID")
    private int id;

    @SerializedName("Name")
    private String name;

    @SerializedName("Short")
    private String abbrevation;

    public Book(String number, String description, int id, String name, String abbrevation) {
        this.number = number;
        this.description = description;
        this.id = id;
        this.name = name;
        this.abbrevation = abbrevation;
    }

}

The answer is quite simple, you have to use the annotation SerializedName to indicated which part of the JSON object is used to parse the JSON object to a Book object:

public class Book {

    @SerializedName("Add1")
    private String number;

    @SerializedName("Description")
    private String description;

    @SerializedName("ID")
    private int id;

    @SerializedName("Name")
    private String name;

    @SerializedName("Short")
    private String abbrevation;

    public Book(String number, String description, int id, String name, String abbrevation) {
        this.number = number;
        this.description = description;
        this.id = id;
        this.name = name;
        this.abbrevation = abbrevation;
    }

}
七月上 2024-11-13 08:54:57

我不确定 GSON 是否知道如何将 JSONObjects 的 JSONArray 映射到 Book 类。我对这个设置有一些观察。

  • 如果您注意到,JSONObjects
    组成 JSONArray 包含
    属性“Add1”和“Short”,但是
    你的 Book 类没有
    具有相同名称的属性。

  • 应注明类型。我是
    猜测“Add1”将被映射
    到数字属性(纯粹是
    猜测),类型为 String
    JSONObject,但它是一个 int
    图书类。

  • 我想知道这种情况是否
    Book 类属性需要
    匹配 JSONObject 的大小写。

  • 您的 Book 类不包含
    公共默认构造函数,我
    认为 GSON 需要映射。

以上只是我的一些建议,不一定正确或完整,因为我以前没有使用过 GSON。

I am not sure that GSON knows how to map your JSONArray of JSONObjects to your Book class. I have a couple observations about this setup.

  • If you notice, the JSONObjects which
    make up the JSONArray contain the
    properties "Add1" and "Short", but
    your Book class does not have
    properties that have the same names.

  • The types should be noted. I am
    guessing that "Add1" is going to map
    to the number property (purely a
    guess), and the type is String in the
    JSONObject, but it is an int in the
    Book class.

  • I am wondering whether the case of
    the Book class properties need to
    match the case of the JSONObject.

  • Your Book class does not contain a
    public default constructor, which I
    think GSON needs to map.

The above are just a couple suggestions I have, which are not necessarily correct or complete as I have not used GSON before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文