Bohdan Samusko
Bohdan Samusko

Reputation: 183

Retrofit 2.0 Can't parse Json nested object

I am using Retrofit 2.0 working with the GitHub API.

So I am interested in obtaining

parent" - > "full_name"

but when I try to run my code this value equals null.

I tried to use many approaches but without any positive results. So I am stuck with this problem.

Below is the JSON response which I want to parse:

    {
      "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 classes:

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.
}

Are my POJO classes are correct? Why I can't get

parent" ->"full_name"

Upvotes: 0

Views: 813

Answers (1)

Shayan_Aryan
Shayan_Aryan

Reputation: 2042

Why are you defining two fields with the same name (="name")? BTW,Your class should be something like this:

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;
     }
}

Upvotes: 1

Related Questions