0

Sample Json -


{"error":
  {"message":"irp-return-error",
    "irp-err":{"ErrorCode":"1015","ErrorMessage":"Invalid Data for this user"}
  }
}

I tried setting the name using @SerializedName Gson annotation

public class Error {
    @SerializedName("irp-err")
    private IrpErr irpErr;
    private String message;

but the call to the json object returns null.

Gson erngson = new Gson();
ErrResponse data = erngson.fromJson(response.readEntity(String.class), ErrResponse.class);
if(data.getError()!=null){
            IrpErr irperr = data.getError().getIrpErr(); //NPE
}
Shan Matt
  • 11
  • 3
  • Please provide a [mcve]. Are you trying to deserialize all of that JSON as `Error` or just the nested value `"error"`? – Savior Mar 14 '21 at 15:03
  • Yeah, this works fine for me. You'll need another (wrapper) class type to hold the `"error"` member. – Savior Mar 14 '21 at 15:09
  • @Savior My problem is similar to [https://stackoverflow.com/questions/43536921/json-response-with-dash-in-the-key-name] where i am using Gson instead of Jackson library. – Shan Matt Mar 14 '21 at 15:24
  • Like I said, your `fromJson` needs to pass a wrapper type that has a field called `error` of type `ErrResponse`. – Savior Mar 15 '21 at 00:02

0 Answers0