How to create POJO for duplicate property name in JSON for Android Retrofit library

710 views Asked by At

I have the following JSON to create POJO and that the POJOs will be used in Android Retrofit library but how to create POJO classes with duplicate JSON property name?

My sample JSON:

{
    "result": {
        "detail": {
            "name": "sample"
        }
    },
    "info": {
        "detail": {
            "user_information": "user"
        }
    }
}

Here JSON object detail is duplicated in info and result JSON objects, if I create detail.java for result and detail_.java for info then get null pointer exception from Retrofit library (I hope, get the exception for property name mismatch with pojo classes).

2

There are 2 answers

2
Harish Gyanani On

You can use this site to convert json into pojo classes. Secondary you can declare Detail class inside Result class and Info class as a inner class. Example :- View.OnClickListener and DialogInterface.OnClickListener. In this example, OnClickListener interface name is same so we need to write outer class name to distinguish.

For your case, this is json converted by site:-

-----------------------------------com.example.Detail.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Detail {

@SerializedName("name")
@Expose
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
-----------------------------------com.example.Detail_.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Detail_ {

@SerializedName("user_information")
@Expose
private String userInformation;

public String getUserInformation() {
return userInformation;
}

public void setUserInformation(String userInformation) {
this.userInformation = userInformation;
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("result")
@Expose
private Result result;
@SerializedName("info")
@Expose
private Info info;

public Result getResult() {
return result;
}

public void setResult(Result result) {
this.result = result;
}

public Info getInfo() {
return info;
}

public void setInfo(Info info) {
this.info = info;
}

}
-----------------------------------com.example.Info.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Info {

@SerializedName("detail")
@Expose
private Detail_ detail;

public Detail_ getDetail() {
return detail;
}

public void setDetail(Detail_ detail) {
this.detail = detail;
}

}
-----------------------------------com.example.Result.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Result {

@SerializedName("detail")
@Expose
private Detail detail;

public Detail getDetail() {
return detail;
}

public void setDetail(Detail detail) {
this.detail = detail;
}

}
0
Rajesh Gosemath On

Create pojo like this using inner classes.

public class JsonResponse{

  public Result result;
  public Info info;

  //Getters and Setters

  public class Result{

      public NameDetails detail;
      //Getters and Setters

      public class NameDetails{

        public String name;
            //Getters and Setters
      }
  }

  public class Info{

      public UserInfoDetails detail;
      //Getters and Setters 

  public class UserInfoDetails {

        public String user_information;
            //Getters and Setters
      } 
  }

}