Gson 反序列化 - 尝试将 JSON 解析为对象
我正在尝试将 JSON 解析为对象。有两个类:用户和配置文件。用户获得了一个 Profile 实例。
所以现在有一个 JSON 来构建用户对象。在这个 JSON 中列出了 User 和 Profile 的属性,如您所见,Profile 和 User 都有一个名为 List 的 HashMap。但是我想用这个 Json 创建用户和配置文件,但我得到了这个异常:
//编辑:
我删除了 Map
。所以现在我没有收到任何错误,每个用户都有一个个人资料 - 但我仍然需要这些地图。 GSON 是否有可能无法区分 json 内的两个列表资源,因为它们具有相同的名称?
//肮脏的黑客解决方案: 使用 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<String, String> links;
public User()
{
this.username = null;
this.slug = null;
this.email = null;
this.emailVerified = null;
this.profile = null;
this.links = new HashMap<String, String>();
}
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<String, String> 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<String, String>();
}
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" : "[email protected]",
"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;
}
我的错误在哪里?我可以将这样的 HashMap 与 GSON 一起使用吗?提前致谢
I am trying to parse a JSON into an Object. There are two classes: User and Profile. User got an instance of Profile.
So now there is one JSON to build an User Object. Inside this JSON are the attributes for the User and Profile listed and as you can see, Profile and User got both a HashMap called List. However i'd like to create the User and the Profile out of this Json, but i got this Exception:
//EDIT:
I removed the Map<String, String> links
from Profile and User. So now I do not get any errors and every User got a Profile - but I still need those Maps. Is it possible that GSON cant differentiate between the two lists ressources inside of the json because they have the same name?
//Dirty Hack Solution:
An ArrayList instead of the HashMap was no problem. However I decided to parse this part of the Json "by hand" to insert the Objects into my 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)
User:
public class User {
private String username;
private String slug;
private String email;
private Boolean emailVerified;
private Profile profile;
Map<String, String> links;
public User()
{
this.username = null;
this.slug = null;
this.email = null;
this.emailVerified = null;
this.profile = null;
this.links = new HashMap<String, String>();
}
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;
}
}
Profile:
public class Profile {
private Map<String, String> 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<String, String>();
}
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;
}
}
An example JSON:
{ "email" : "[email protected]",
"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"
}
Accessing Method:
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;
}
Where is my mistake? And can i use a HashMap like this with GSON? Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Gson 反序列化器类。它们非常简单:
为了使这项工作有效,您必须确保解析器不会尝试序列化侵权对象
(在本例中是您的地图)。我会将您的地图对象重命名为 _links 或类似名称,以便序列化程序将跳过它。对您的个人资料也执行与此示例相同的操作。
完成后,您必须反序列化它并确保将反序列化器包含在 gson 对象中:
Use a Gson Deserializer class. they are pretty straightforward:
To make this work, you have to make sure the parser isn't going to try and serialize the infringing object
(in this case your Map). I would rename your map object to _links or somesuch so the serializer will skip over it. Do the same thing as this example for your Profile as well.
Once you've done that you have to deserialize it and make sure to include the deserializer in the gson object: