简体   繁体   English

根据JSON字段的值,具有不同类型对象的Gson流API

[英]Gson streaming API with different types of objects according to value of JSON field

I'm trying to parse the Wikidata JSON dump using the Gson streaming API, since the file is around 70GB of json. 我正在尝试使用Gson流API解析Wikidata JSON转储 ,因为该文件的JSON大小约为70GB。 The overall structure of the file is as follows: 该文件的总体结构如下:

[
{"type":"item",... other fields ...},
{"type":"property",... other fields ...},
.....
]

It is an array of objects in which each object can be of type item or property and I would like to instantiate a different class (namely I have a corresponding Item and Property class in my Java code) according to the object that I encounter. 它是一个对象数组,其中每个对象可以是itemproperty类型,我想根据遇到的对象实例化一个不同的类(即,我的Java代码中有一个对应的Item和Property类)。

Basically, I'd like to look at the type field and then parse the following JSON accordingly. 基本上,我想查看type字段,然后相应地解析以下JSON。 Since the JsonReader doesn't seem to provide a getNextJsonObject() or similar function, is there a way to do this besides preprocessing the whole file and splitting the entries into two separate ones? 由于JsonReader似乎没有提供getNextJsonObject()或类似的函数,除了预处理整个文件并将条目分成两个单独的条目之外,还有其他方法可以这样做吗? The file is so big that I'd like to avoid the extra preprocessing step when I could do everything on the fly. 该文件太大,以至于在我可以即时执行所有操作时,我想避免额外的预处理步骤。

I actually found a very easy solution after a bit of thinking. 经过一番思考,我实际上找到了一个非常简单的解决方案。 The Gson API provides the method: Gson API提供了以下方法:

Gson.fromJson(JsonReader reader, Class class)

This will read the next object from the reader and deserialize to the class you pass as parameter. 这将从读取器读取下一个对象,并将其反序列化为您作为参数传递的类。 Since in my case I don't know which class to serialize to I can do the following: 因为在我的情况下,我不知道要序列化到哪个类,所以可以执行以下操作:

JsonObject asd = gson.fromJson(reader, JsonObject.class);
if (asd.get("type").getAsString().equals("item")) {
    // Instantiate item
} else {
    // Instantiate property
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM