如何将GSON反序列化为相应的泛型Java类型? [英] How to deserialize JSON with GSON into corresponding generic Java types?

查看:253
本文介绍了如何将GSON反序列化为相应的泛型Java类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个JSON对象(使用GSON,因为我已经用它来对对象进行搜索)反序列化为一个通用的Map Map< String,Object> 。它应该创建与相应的JSON类型相对应的类型的对象,即这个JSON对象

  {
docID :a12345,
ation:[1,2,3],
title:{de:German Title,
en:English Title}
}

应该反序列化为 Map< String,Object> 包含以下类型的条目:
String , String ),( String List< String> ),( String Map< String,String> )。



以下代码:

  class Dictionary_ extends HashMap< String,Object> {
//不需要
};

private static final GsonBuilder GSON_BUILDER = new GsonBuilder();

public Map< String,Object> deserializeJsonString(String jsonString){
Dictionary_ d = new Dictionary();
return GSON_BUILDER.create()。fromJson(jsonString,d.getClass());
}

但是生成的对象类型如下所示:

String Object ),( String Object ),( String , Object
,我无法将这些对象转换为相应的真实类型,因为我得到了类转换异常。



我无法使用POJO,因为我收到的数据在类成员方面没有预定义的结构。因此,我如何能够根据泛型Java类型反序列化这些泛型数据?

p>

更新:

我只是尝试使用JSON Simple来实现它,它可以非常容易地通过以下代码:


  Object ret = JSONValue.parse(jsonString); 
return(List< Map< String,Object>>)ret;

为什么GSON不能这么简单?

解决方案

JSON.simple反序列化的结构是一个Map,其中每个条目键都是一个String,每个条目值都是简单的数据类型或另一个Map。

不幸的是,使用Gson反序列化这样的结构并不像我想的那样简单,只需要几行代码。我在 http://programmerbruce.blogspot.com上发布了一个例子/2011/06/gson-v-jackson.html 。 (搜索短语Gson代码将任何JSON对象转换为Map直接跳转到相关示例。)

该博客帖子还包括使用杰克逊只用一行代码完成同样的任务。 (我强烈建议尽可能放弃杰森,只要有可能。)


I want to deserialize a JSON object (using GSON, because I already use it for searializing objects) to a general Map of type Map<String, Object>. It should create Objects of types that do correspond to the according JSON types, i.e. this JSON Object

{ 
    "docID" : "a12345",
    "relation" : ["1", "2", "3"],
    "title" : { "de" : "German Title",
                "en" : "English Title"}
}

should be deserialized in a Map<String, Object> with entries of the following types:
(String, String), (String, List<String>), (String, Map<String, String>).

I tried to use the following code:

class Dictionary_ extends HashMap<String, Object> {
    // nothing to do
};

private static final GsonBuilder GSON_BUILDER = new GsonBuilder();

public Map<String, Object> deserializeJsonString(String jsonString) {
    Dictionary_ d = new Dictionary();
    return GSON_BUILDER.create().fromJson(jsonString, d.getClass());
}

But then the resulting types of created Objects are as follows:
(String, Object), (String, Object), (String, Object) and I am not able to cast these objects into the corresponding "real" types, because I get class cast exceptions.

I am not able to use POJOs, since the data I am receiving does not have predefined structure in terms of class members.

So, how am I able to deserialize such generic data in according generic Java types?

Update:
I just tried to implement it using JSON Simple, and it works out of the box very easily by the following code:

        Object ret = JSONValue.parse(jsonString);
        return (List<Map<String, Object>>) ret;

Why does it not work that easy with GSON???

解决方案

The structure that JSON.simple deserialized into was a Map, where each entry key is a String, and each entry value is either a simple data type or another Map.

Deserializing to such a structure with Gson is unfortunately not as simple as I think it should be, taking a few dozen lines of code. I posted an example of doing so at http://programmerbruce.blogspot.com/2011/06/gson-v-jackson.html. (Search for the phrase "Gson Code to turn any JSON object into a Map" to jump straight to the relevant example.)

That blog post also includes an example of using Jackson to accomplish the same task with just one line of code. (I highly recommend abandoning Gson in favor of Jackson, whenever possible.)

这篇关于如何将GSON反序列化为相应的泛型Java类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆