0

I'm trying to convert a JSON to GSON I'm not sure this is the best structure.

All responses consist of code, msg, and data structure. But the internal structure of data can vary.

Do I have to create a response object even if there is only one value in the data like this?

If you know a better way than this, please advise me. thank you :)

{
"code": "000",
"msg": "okay",
"data": {
    "myCartCount": "0"
}
}

this is my reponse object

public class BaseResponse{
  public String code;
  public String msg;
}

public class MyCartResponse extends BaseResponse{
   @SerializedName("data")
   public MyCart myCart;
}

public class MyCart{
  @SerializedName("myCartCount")
  public String count;
}
walnuty
  • 23
  • 4

1 Answers1

0

Use this annotation @SerializedName and @Expose

public class BaseResponse{
  @SerializedName("code")
  @Expose
  public String code;
  @SerializedName("msg")
  @Expose
  public String msg;

   @SerializedName("data")
   @Expose
   public MyCartResponse myCart;
}

public class MyCartResponse {
  @SerializedName("myCartCount")
  public String count;
}
Haseeb Mirza
  • 432
  • 2
  • 7