我的 Android 应用程序的 REST 服务遇到问题。
我从服务器得到了以下 json 响应:
{
"0": {
"id": 1,
"name": "Some Guy",
"email": "example1@example.com"
},
"1": {
"id": 2,
"name": "Person Face",
"email": "example2@example.com"
},
"3": {
"id": 3,
"name": "Scotty",
"email": "example3@example.com",
"fact": {
"hobbies": ["fartings", "bikes"]
}
}
}
我的对象是:
用户类:
public class User {
@SerializedName("id")
private
int id;
@SerializedName("name")
private
String name;
@SerializedName("email")
private
String email;
@SerializedName("fact")
private
Fact fact;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Fact getFact() {
return fact;
}
public void setFact(Fact fact) {
this.fact = fact;
}
public User(){}
}
事实类:
public class Fact {
@SerializedName("hobbies")
private List<Hobbies> hobbies;
public List<Hobbies> getHobbies() {
return hobbies;
}
public void setHobbies(List<Hobbies> hobbies) {
this.hobbies = hobbies;
}
public Fact(){}
}
兴趣类:
public class Hobbies {
private String hobby;
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public Hobbies(){}
}
当我在我的应用中使用以下代码时:
private User jsonToUser(String result){
User users = null;
if(result != null && result.length() > 0){
Gson gson = new GsonBuilder().create();
users = gson.fromJson(result, User.class);
}
return users;
}
函数返回的对象由空值填充。我尝试使用空的类 Users 并扩展 ArrayList
public class Users extends ArrayList<User> {
//
}
应用程序给我错误:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
我希望将它用于:
ArrayAdapter<User> adapter = new ArrayAdapter<User>(activity, android.R.layout.simple_list_item_1, users);
你能告诉我我做错了什么吗?它适用于我的 Twitter 时间轴应用程序,但不适用于此。
最佳答案
你的代码 users = gson.fromJson(result, User.class);
如果你想转换一个 JSON 字符串,比如 { "id": 1, "name": "Some Guy", "email": "example1@example.com"
到 one User
对象。
但是像你这样的 JSON 字符串
{
"0": {
"id": 1,
"name": "Some Guy",
"email": "example1@example.com"
},
...
}
被解释为 User
对象的数组 (或 HashMap?!)。
使用数组尝试以下操作:
users = gson.fromJson(result, User[].class);
或者(如果 GSON 将其解释为 HashMap
):
users = gson.fromJson(result, HashMap<String, User>.class);
更优雅的方式 使用 Collections example 中的代码来自 Gson 用户指南将使用 Collection:
Type collectionType = new TypeToken<Collection<User>>(){}.getType();
Collection<User> users= gson.fromJson(result, collectionType);
在“Collection 限制”部分写了以下内容:
While deserializing, Collection must be of a specific generic type
我不确定,但这可能意味着您必须将 collectionType
设置为使用 List
而不是 Collection
作为特定类型。
或(如果 GSON 将其解释为 HashMap
):
Type hashMapType = new TypeToken<HashMap<String, User>>(){}.getType();
HashMap<String, User> users= gson.fromJson(result, hashMapType);
祝你好运 =)
编辑
我对最后一个解决方案的尝试是成功的:
public class User {
private String id, name, email;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
public static void main(String[] args) {
String result = ""
+ "{\"0\": { \"id\": 1, \"name\": \"Some Guy\", \"email\": \"example1@example.com\"},"
+ "\"1\": { \"id\": 2, \"name\": \"Person Face\", \"email\": \"example2@example.com\"}"
+ "}";
Gson gson = new Gson();
Type hashMapType = new TypeToken<HashMap<String, User>>() {
}.getType();
HashMap<String, User> users = gson.fromJson(result, hashMapType);
for (String key : users.keySet()) {
printUser(users.get(key));
}
}
private static void printUser(User user) {
System.out.printf("%s %s %s\n", user.getId(), user.getName(),
user.getEmail());
}
关于java - Android 和 json 使用嵌套对象的 REST 服务中的 gson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22970087/