Gson反序列化 – 尝试将JSON解析为Object

我试图将JSON解析为Object。 有两个类:User和Profile。 用户获得了个人资料的实例。

所以现在有一个JSON来构建用户对象。 在这个JSON中是列出的User和Profile的属性,正如你所看到的,Profile和User都得到了一个名为List的HashMap。 但是,我想从这个Json创建用户和配置文件,但我有这个例外:

//编辑:

我从Profile和User中删除了Map links 。 所以现在我没有得到任何错误,每个用户都有一个配置文件 – 但我仍然需要这些地图。 是否有可能GSON无法区分json中的两个列表资源,因为它们具有相同的名称?

// Dirty Hack解决方案:ArrayList而不是HashMap没问题。 但是我决定“手动”解析Json的这一部分,将对象插入到我的HashMap中。

 01-03 05:27:59.580: E/AndroidRuntime(4313): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 12 01-03 05:27:59.580: E/AndroidRuntime(4313): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:180) 

用户:

 public class User { private String username; private String slug; private String email; private Boolean emailVerified; private Profile profile; Map links; public User() { this.username = null; this.slug = null; this.email = null; this.emailVerified = null; this.profile = null; this.links = new HashMap(); } public String getUsername(){ return this.username; } public String getSlug(){ return this.slug; } public String getEmail(){ return this.email; } public Boolean getEmailVerified(){ return this.emailVerified; } public Profile getProfile(){ return this.profile; } } 

轮廓:

 public class Profile { private Map links; private String name; private String description; private String gender; private String status; private String timezone; private Bitmap icon; public Profile() { this.name = null; this.description = null; this.gender = null; this.status = null; this.timezone = null; this.links = new HashMap(); } public String getName(){ return this.name; } public String getDescription(){ return this.description; } public String getGender(){ return this.gender; } public String getStatus(){ return this.status; } public String getTimezone(){ return this.timezone; } } 

一个例子JSON:

 { "email" : "foo@bar.com", "emailVerified" : true, "links" : [ { "href" : "http://xxx.de/api/users/4f3a73004bb67751bc000011", "rel" : "self" }, { "href" : "http://xxx.de:/api/users/4f3a73004bb67751bc000011/followers", "rel" : "https://xxx.com/rels/collection/follower" }, { "href" : "http://xxx.de/api/users/4f3a73004bb67751bc000011/friends", "rel" : "https://xxx.com/rels/collection/friend" }, { "href" : "http://xxx.de/api/users/4f3a73004bb67751bc000011/activity_stream", "rel" : "https://xxx.com/rels/activity_stream" } ], "profile" : { "description" : "", "gender" : "male", "links" : [ { "href" : "xxx.de/uploads/profile_images/xxx.png", "rel" : "https://xxx.com/rels/image" }, { "href" : "http://xxx.de/api/users/xxx/profile", "rel" : "self" } ], "name" : "Foo Bar", "status" : "Status", "timezone" : "CET" }, "slug" : "foobaar", "username" : "foobaar" } 

访问方法:

 public static User parseUser(String json) { JSONObject jsonObject; Gson gson = new Gson(); try { jsonObject = new JSONObject(json); Log.v(TAG,jsonObject.toString(2)); User u = gson.fromJson(jsonObject.toString(), User.class); return u; } catch (JSONException e){ Log.e(TAG, "There was an error parsing the JSON (USER)" + e); } return null; } 

我的错误在哪里? 我可以使用GSON这样的HashMap吗? 提前致谢

使用Gson反序列化器类。 它们非常简单:

要使这项工作,您必须确保解析器不会尝试序列化侵权对象(在本例中为您的Map)。 我会将你的地图对象重命名为_links或者某些东西,所以序列化程序会跳过它。 对于您的个人资料,请执行与此示例相同的操作。

完成后,必须对其进行反序列化,并确保在gson对象中包含反序列化器:

  User u; GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapter(User.class, new UserDeserializer()); Gson g = gb.create(); u = g.fromJson(json, User.class); public class UserDeserializer implements JsonDeserializer { @Override public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { User u = g.fromJson(json, User.class); JsonObject jo = (JsonObject)json; JsonElement je = jo.get("links"); //iterate through the je element to fill your map. } }