Retrofit 2.0无法解析Json嵌套对象

时间:2015-12-07 21:11:36

标签: java android json gson retrofit

我正在使用 Retrofit 2.0 使用 GitHub API

所以我有兴趣获得

parent" - > "full_name"

但是当我尝试运行我的代码时,此值等于null

我试图使用很多方法,但没有任何积极的结果。所以我遇到了这个问题。

以下是我要解析的JSON响应:

    {
      "id": 45136403,
      "name": "android_guides",
      "full_name": "BohdanSamusko/android_guides",
      "owner": {
        "login": "BohdanSamusko",
      },
      "parent": {
        "name": "android_guides",
        "full_name": "codepath/android_guides",
        "owner": {
          "login": "codepath",
          "id": 3710273,
        },
      },
    }

POJO课程

public class Repo {
   @SerializedName("name") // name of repository
   private String name = ""; 

   @SerializedName("full_name") // full name of repository
   private String name = ""; 

    @SerializedName("parent") // this is the nested object which I want to parse
    private Parent parent = "";
}

class Parent{
   @SerializedName("full_name")
   private String full_name = ""; // full name of repository parent. This value I want to parse.
}

我的POJO课程是否正确?为什么我无法获得

parent" ->"full_name"

1 个答案:

答案 0 :(得分:1)

为什么要定义两个具有相同名称的字段(=" name")? 顺便说一句,你的课应该是这样的:

public class Repo{
     private long id;
     private String name;
     private String full_name;
     private Owner owner;
     private Parent parent;

     public class Parent{
          private String name;
          private String full_name;
          private Owner owner;
     }

     public class Owner{
          private long id;
          private String login;
     }
}