如何访问对象内对象的属性?

时间:2017-07-05 14:04:26

标签: java mongodb object gson

我有像这样的物体Pelicula:

public class Pelicula {
    private String popularity;
    private Object belongs_to_collection;

    public Pelicula() {
        this.popularity = popularity;
    }


    public String getPopularity() {
        return popularity;
    }   

    public Object getBelongs_to_collection() {
        return belongs_to_collection;
    }

}

此对象是使用Gson将mongodb文档转换为对象Pelicula,其中包含一些字符串和对象belongs_to_collection。

{
  "adult": false,
  "backdrop_path": "/8SqBiesvo1rh9P1hbJTmnVum6jv.jpg",
  "belongs_to_collection": {
    "id": 304378,
    "name": "Independence Day - Colección",
    "poster_path": "/diCxphvzqas0Tr5eq7tLUzg5M7d.jpg",
    "backdrop_path": "/p7oqa94XgNGVMazXwR49QfyGgtx.jpg"
  },
  "budget": 165000000 
...

为了创建对象,我使用了这个函数:

 public List getAllMovies() {
        List<Pelicula> allMovies = new ArrayList();
        Pelicula movie;
        for (Document p : getCollection().find()) {
            movie = getGson().fromJson(p.toJson(), Pelicula.class);
            allMovies.add(movie);

        }
        System.out.println(allMovies.get(0).getPopularity());
        return allMovies;
    }

如您所见,我可以打印该物业的人气。另外,我可以使用toString打印对象Pelicula的对象belongs_to_collection。

问题是:我是否可以逐个访问belongs_to_collection的属性而无需创建继承Pelicula的对象belongs_to_collection?

0 个答案:

没有答案