这个JSON字符串的Java对象应该是什么?

时间:2015-04-17 08:23:42

标签: java json pojo

{"userId":"vincent","favTracks":{"favourite":"15","unFavourite":"121"}}

上述JSON字符串的Java对象是什么?

1 个答案:

答案 0 :(得分:0)

这实际上取决于你想要如何映射它。如果您使用Jackson,例如,使用默认的映射设置,您的类可能如下所示:

class MyObject {
    private String userId;
    private FavTracks favTracks;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public FavTracks getFavTracks() {
        return favTracks;
    }

    public void setFavTracks(FavTracks favTracks) {
        this.favTracks = favTracks;
    }
}

class FavTracks {
    private String favourite;
    private String unFavourite;

    public String getFavourite() {
        return favourite;
    }

    public void setFavourite(String favourite) {
        this.favourite = favourite;
    }

    public String getUnFavourite() {
        return unFavourite;
    }

    public void setUnFavourite(String unFavourite) {
        this.unFavourite = unFavourite;
    }
}

一句话:在您当前的示例中,favouriteunFavourite属性属于字符串类型。也许数字类型更合适?