GSON.fromJson嵌套Java对象

时间:2012-10-22 08:42:41

标签: java android facebook gson

我正在尝试更新我的Android应用程序,以适应最近的Facebook更改与图片。以前我只是为用户照片返回一个字符串,但没有返回一个数组。我已经更新了我的代码,但数据只是返回null,有人能指出我正确的方向吗?

由于

GSON致电

Gson gson = new GsonBuilder().create();
FBFriendResponse resp = gson.fromJson(response, FBFriendResponse.class);

新的Facebook数据结构

"picture": {
    "data": {
       "url": "######.jpg",
       "is_silhouette": false
    }
 }


public class FBFriend implements Comparable<FBFriend>
{
    public String id, name, gender;
    public FBFriendPicture picture;

    @Override
    public int compareTo(FBFriend another) {
        return this.name.compareTo(another.name);       
    }

}
public class FBFriendPicture 
{
    public FBFriendPictureData pictureData;
}

public class FBFriendPictureData 
{
    public String friendPictureURL;
    public Boolean friendPictureIssilhouette;
}

1 个答案:

答案 0 :(得分:0)

解决了这个问题。这是由于类中使用了错误的名称

public class FBFriend implements Comparable<FBFriend>
{
    public String id, name, gender;
    public FBFriendPicture picture;

    @Override
    public int compareTo(FBFriend another) {
        return this.name.compareTo(another.name);       
    }

}
public class FBFriendPicture 
{
    public FBFriendPictureData data;
}

public class FBFriendPictureData 
{
    public String url;
    public Boolean is_silhouette;
}