我正在使用GSON反序列化一些JSON文件。这是我编写的反序列化方法,我读了JSON文件并将整个东西存储为我传递给此方法的字符串。此方法适用于与此项目相关的5个JSON文件中的4个。
protected ArrayList<Entry> deserialize(String json) throws Exception
{
ArrayList<Entry> list = new ArrayList<Entry>( );
JsonParser parser = new JsonParser();
JsonArray jarray = (JsonArray) parser.parse(json);
for (int i = 0; i < jarray.size(); i++)
{
// Parse out the brand
JsonObject jentry = (JsonObject) jarray.get(i);
JsonPrimitive jbrand = jentry.getAsJsonPrimitive("brand");
String className = jbrand.getAsString();
Entry entry = (Entry) gson.fromJson(jentry, Class.forName(className));
list.add(entry);
}
return list;
}
这是我解析并放入字符串的JSON文件,有几个对象绑定到'jentry',但我只包含一个。如果它看起来很奇怪,可能是因为我一直在使用firefox插件来查看JSON文件并从该插件中复制/粘贴。
[
*
-
{
o pattern: "3 5 * * 1-5"
o starts: 1288249260913
o ends: 1291125660913
o skipHolidays: false
o lastFired: 1289988180395
o
-
template: {
+ location: ""
+ damageCause: ""
+ signed: false
+ signedBy: ""
+ approvedBy: "Ralph"
+ requestedBy: "Ralph"
+ estHours: 0
+ actHours: 0
+ chargeTo: ""
+ priority: "ROUTINE"
+ reason: ""
+ materials: ""
+ serviceId: 1
+ descr: "HELP WITH LEAVES,BLOW LEAVES IN YOUR AREA NEAR DRAINS Check for garbage. [sp] Mow and weedeat where needed in your area. [sp] Work on leaves where needed. [wi]"
+ comments: [ ]
+ futureId: 3
+ inventoryId: -1
+
-
trail: [
#
-
{
* stamp: 1288026816857
* status: "OPEN"
* byId: 2
}
#
-
{
* stamp: 1288026889374
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1288194095170
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1288287964481
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1288785076532
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1288797119525
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1289307416921
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1289308339165
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1289834523635
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1289847660913
* status: "DISPATCHED"
* byId: 2
}
]
+ requestDate: 1289329260913
+ assignedDate: 1288029660912
+ supplies: [ ]
+ id: 3
+ updateDate: 1289847660913
+ createUserId: 2
+ updateUserId: 2
+ createDate: 1288026816857
+ brand: "org.workplicity.marist.grounds.GroundsRequest"
}
o workSlateId: 16
o serviceId: 1
o enabled: false
o id: 3
o updateDate: 1291235385719
o createUserId: 2
o updateUserId: 2
o createDate: 1288026889373
o brand: "org.workplicity.entry.event.Weekdays"
}
问题是,当GSON变回JSON(序列化?)时,它会遗漏一些字段。这是输出,相关的缺失行是'template:'下面的所有内容以及'serviceID:'上面的内容,我将继续并再次包含整个对象。
[
*
-
{
o pattern: "3 5 * * 1-5"
o starts: 1288249260913
o ends: 1291125660913
o skipHolidays: false
o lastFired: 1289988180395
o
-
template: {
+ serviceId: 1
+ descr: "HELP WITH LEAVES,BLOW LEAVES IN YOUR AREA NEAR DRAINS Check for garbage. [sp] Mow and weedeat where needed in your area. [sp] Work on leaves where needed. [wi]"
+ comments: [ ]
+ futureId: 3
+ inventoryId: -1
+
-
trail: [
#
-
{
* stamp: 1288026816857
* status: "OPEN"
* byId: 2
}
#
-
{
* stamp: 1288026889374
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1288194095170
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1288287964481
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1288785076532
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1288797119525
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1289307416921
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1289308339165
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1289834523635
* status: "DISPATCHED"
* byId: 2
}
#
-
{
* stamp: 1289847660913
* status: "DISPATCHED"
* byId: 2
}
]
+ requestDate: 1289329260913
+ assignedDate: 1288029660912
+ supplies: [ ]
+ id: 3
+ updateDate: 1289847660913
+ createUserId: 2
+ updateUserId: 2
+ createDate: 1288026816857
+ brand: "org.workplicity.marist.grounds.GroundsRequest"
}
o workSlateId: 16
o serviceId: 1
o enabled: false
o id: 3
o updateDate: 1299694066807
o createUserId: 2
o updateUserId: 2
o createDate: 1288026889373
o brand: "org.workplicity.entry.event.Weekdays"
}
对于JSON文件中的每个对象都会发生这种情况。 NetBeans中的调试显示JsonObject'jentry'具有哈希表,该哈希表具有JSON字符串中每个数据成员的对应键值对;并且'模板'作为哈希表存储在此哈希表中,这可能是也可能不是我真正无法找到的问题。
现在,当我最初在问题JSON文件上运行此方法时,我在这一行得到了一个异常:
Entry entry = (Entry) gson.fromJson(jentry, Class.forName(className));
问题是这个特定JSON文件涉及的特定类没有no-args构造函数,所以我必须将一些InstanceCreator注册到GSON构建器,如下所示:
gsonBuilder.registerTypeAdapter(Weekdays.class, new WeekdaysInstanceCreator());
gsonBuilder.registerTypeAdapter(Once.class, new OnceInstanceCreator());
在我这样做之后,异常停止被抛出,一切似乎都有效,当然减去缺少的字段。
所以我就是这样,我真的不知道出了什么问题。任何帮助都非常感谢。
答案 0 :(得分:2)
当Gson删除JSON中存在的字段时,通常是因为您反序列化的类没有定义该字段。 如果使用基类类型对其进行反序列化,则可能会发生这种情况。 你能检查一下org.workplicity.marist.grounds.GroundRequest是否包含你所指的字段吗?
答案 1 :(得分:0)
请尝试下载最新版本的Gson,1.7。它包含许多可能对您有所帮助的新功能。请参阅http://groups.google.com/group/google-gson/browse_thread/thread/6272c9be58676e47#。
首先,不再需要Instance Creator,因为Gson能够在不使用默认构造函数或实例创建者的情况下为任何类实例自动分配堆空间。
对于字段的跳过,默认情况下,如果Gson是“静态”,“瞬态”或“内部类”,它们将跳过字段。
答案 2 :(得分:0)
NetBeans中的调试显示JsonObject'jentry'有一个散列表,其中包含JSON字符串中每个数据成员的对应键值对;并且'模板'作为哈希表存储在此哈希表中,这可能是也可能不是我真正无法找到的问题。
这是正常的。 Gson最初将JSON对象读入JsonObject
,其中JSON元素名称和值存储在LinkedHashMap
中。因此,如果JSON结构包含对象中的对象,那么初始结构Gson将JSON读入将是JsonObject
,其中LinkedHashMap
,其中一个条目的值为另一个JsonObject
LinkedHashMap
。{/ p>
原始帖子没有明确说明JsonObjects
中的地图内容出乎意料地缺乏。它只描述了序列化反序列化数据后发现有些内容丢失了。由于没有提供足够的信息来重现问题,人们只能猜测问题可能是什么。并且inder的猜测看起来和任何一样好。我没有看到任何迹象表明Gson的缺陷。
关于问题标题的特定声明“使用GSON的JSON反序列化从散列映射中的散列映射中跳过数据成员”,从问题的其余内容开始,我认为原始问题实际上并不意味着目标Java结构是地图中的地图。如果目标Java结构是地图中的地图,那么我无法重现Gson神秘地跳过某些字段的场景,只要JSON元素正确绑定到Java字段,并且处理{{3}提供(使用自定义反序列化)。