GSON:反序列化时删除不必要的父对象

Mar*_*ark 6 java gson json-deserialization

我试图使用GSON反序列化JSON数组.我的所有嵌套对象都嵌入在"嵌入"对象中.

{
    "Book": {
        "name": "Book 1",
        "published": 1999,
        "links": {
          "url": "www.book1.com"
        },
        "embedded": {
            "Author": {
                "name": "John Doe",
                "links": {
                    "url": "www.johndoe.com"
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我也可能有这样的情况:

{
    "Book": {
        "name": "Book 1",
        "published": 1999,
        "links": {
          "url": "www.book1.com"
        },
        "embedded": {
            "Publisher": {
                "name": "Publishing Company",
                "links": {
                    "url": "www.publishingcompany.com"
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个非常简单的例子.我的一些对象可以嵌套2或3级深度,并且所有对象都在"嵌入"对象中.此外,每个对象在"链接"对象中都有一个嵌套的"url".我有大约20个不同的模型对象,每个对象都有几个字段,每个对象都有"嵌入"对象.我开始为每个模型编写自定义反序列化器,但这似乎忽略了使用gson的全部意义,我可能并不总是知道嵌入对象是什么.

我找到了这个答案,但它是用于序列化对象.我一直试图弄清楚这一段时间,并没有找到任何有用的东西.

My Book模型如下所示:

public class Book {
    String name;
    int published;
    String url;
    Author author;
    Publisher publisher;
}
Run Code Online (Sandbox Code Playgroud)

作者类:

public class Author {
    String name;
    String url;
}
Run Code Online (Sandbox Code Playgroud)

发布者类:

public class Publisher {
    String name;
    String url;
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我的Book deserializer:

public class BookDeserializer implements JsonDeserializer<Book> {
    @Override
    public Book deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

        final JsonObject jsonObject = json.getAsJsonObject();

        Book book = new Book();
        book.setName(jsonObject.get("name").getAsString());
        book.setPublished(jsonObject.get("published").getAsInt());
        String url = jsonObject.getAsJsonObject("links").get("url").getAsString();
        book.setUrl(url);

        // 1) How to get rid of this and skip to the "real" nested object?
        final JsonObject embeddedObject = jsonObject.getAsJsonObject("embedded");

        // 2) See what the "embedded" object actually is.
        String embeddedModel;
        Set<Map.Entry<String, JsonElement>> entrySet = embeddedObject.entrySet();
        for (Map.Entry<String, JsonElement> entry : entrySet) {

            // Author or Publisher
            embeddedModel = entry.getKey();
        }

        // We have the model's key, now add code here to deserialize whatever the object is

        return book;
    }
}
Run Code Online (Sandbox Code Playgroud)

我仍然需要解析json并为Book设置每个字段.然后,我将不得不添加代码来确定和使用嵌套对象的正确反序列化器.看起来我仍然需要每个对象的自定义反序列化器来获取"url".我对gson相当新,所以也许我只是忽略了一些东西,但似乎我不妨手动解析所有的json,甚至不使用gson.也许有办法让json变平?

关于如何解析这个并仍然使用gson的便利性的任何想法,或者这甚至可能吗?也许杰克逊能更好地处理这件事

use*_*041 1

创建一个名为embedded的类并将其添加为Book中的字段:

public class Book {
    String name;
    int published;
    Embedded embedded;
}
Run Code Online (Sandbox Code Playgroud)

然后创建一个嵌入类:

public class Embedded {
    Author Author;
    Publisher Publisher;
}
Run Code Online (Sandbox Code Playgroud)

只需根据 JSON 建模您的类