I am trying to deserialize a Json String using GSONbut after deserialization all values appear as Null. This is the Json String (I obtain this String from a server through a Rest API):
{"getInformationResult":{
"BE_Active":false,"BE_Owner_ID":0,"BT_ID":0,"CLC_Sale":0,"CLC_Shared_Balance":false,"Cash":false,"Client_Active":false,"Client_Alias":null,"Client_ID":0,"Clients_Balance_By_Brand":null,"Contact_Active":false,"Contact_ID":0,"Credit":false,"Currency_ID":0,"Error_Codes":null,"Errors" :[{"Error_Code":"101","Error_Description":"Login: Invalid User or Password","Error_ID":27,"Error_Name":"Invalid User Or Password"}],"Father_ID":0,"Labels":null,"Language_ID":0,"Parent_ID":0,"Schedule_Time_End":null,"Schedule_Time_Start":null,Server_Date_Time":null,"Server_TZ_ISO_Notation":null,"TT_ID":0,"TZ_ISO_Notation":null,"Template_ID":0,"User_Active":false,"User_Email":null,"User_Name":null}}
And this is what I am Using to deserialize:
Gson gson = new Gson();
getInformationResult getInformationResult = new getInformationResult();
getInformationResult respuesta = gson.fromJson(response, getInformationResult.class);
public class getInformationResult {
@SerializedName("Errors")
public Errors Errors;
@SerializedName("User_Name")
public String User_Name;
@SerializedName("BE_Active")
public String BE_Active;
@SerializedName("BE_Owner_ID")
public String BE_Owner_ID;
@SerializedName("BT_ID")
public String BT_ID;
@SerializedName("CLC_Sale")
public String CLC_Sale;
@SerializedName("CLC_Shared_Balance")
public String CLC_Shared_Balance;
@SerializedName("Cash")
public String Cash;
@SerializedName("Client_Active")
public String Client_Active;
@SerializedName("Client_Alias")
public String Client_Alias;
@SerializedName("Client_ID")
public String Client_ID;
@SerializedName("Clients_Balance_By_Brand")
public String Clients_Balance_By_Brand;
@SerializedName("Contact_Active")
public String Contact_Active;
@SerializedName("Contact_ID")
public String Contact_ID;
@SerializedName("Credit")
public String Credit;
@SerializedName("Currency")
public String Currency;
@SerializedName("Error_Codes")
public String Error_Codes;
@SerializedName("Father_ID")
public String Father_ID;
@SerializedName("Labels")
public String Labels;
@SerializedName("Language_ID")
public String Language_ID;
@SerializedName("Parent_ID")
public String Parent_ID;
@SerializedName("Schedule_Time_End")
public String Schedule_Time_End;
@SerializedName("Schedule_Time_Start")
public String Schedule_Time_Start;
@SerializedName("Server_Date_Time")
public String Server_Date_Time;
@SerializedName("Server_TZ_ISO_Notation")
public String Server_TZ_ISO_Notation;
@SerializedName("TZ_ISO_Notation")
public String TZ_ISO_Notation;
@SerializedName("TT_ID")
public String TT_ID;
@SerializedName("Template_ID")
public String Template_ID;
@SerializedName("User_Active")
public String User_Active;
@SerializedName("User_Email")
public String User_Email;
public getInformationResult(Errors Errors, String User_Name,String BE_Active,String BE_Owner_ID, String BT_ID,String CLC_Sale,String CLC_Shared_Balance, String Cash,
String Client_Active, String Client_Alias,String Client_ID,String Clients_Balance_By_Brand, String Server_TZ_ISO_Notation,String Contact_Active,
String Contact_ID, String Credit, String Currency,String Error_Codes,String Father_ID,String Labels,String Language_ID,String Parent_ID,
String Schedule_Time_End,String Schedule_Time_Start,String Server_Date_Time, String TZ_ISO_Notation,String TT_ID, String Template_ID, String User_Active, String User_Email ) {
this.Errors = Errors;
this.User_Name = User_Name;
this.BE_Active = BE_Active;
this.BE_Owner_ID = BE_Owner_ID;
this.BT_ID=BT_ID;
this.CLC_Sale = CLC_Sale;
this.CLC_Shared_Balance=CLC_Shared_Balance;
this.Cash=Cash;
this.Client_Active=Client_Active;
this.Client_Alias = Client_Alias;
this.Client_ID = Client_ID;
this.Clients_Balance_By_Brand = Clients_Balance_By_Brand;
this.Server_TZ_ISO_Notation = Server_TZ_ISO_Notation;
this.Contact_Active=Contact_Active;
this.Contact_ID=Contact_ID;
this.Credit=Credit;
this.Currency=Currency;
this.Error_Codes=Error_Codes;
this.Father_ID=Father_ID;
this.Labels=Labels;
this.Language_ID=Language_ID;
this.Parent_ID=Parent_ID;
this.Schedule_Time_End=Schedule_Time_End;
this.Schedule_Time_Start=Schedule_Time_Start;
this.TZ_ISO_Notation=TZ_ISO_Notation;
this.TT_ID=TT_ID;
this.Server_Date_Time=Server_Date_Time;
this.Template_ID=Template_ID;
this.User_Active=User_Active;
this.User_Email=User_Email;
}
@NonNull
@Override
public String toString() {
return new Gson().toJson(this);
}
}
2
Answers
In case anybody needs the answer, I solved my issue doing the code like this instead:
public class getInformationResult {
}
public class Errors {
}
public class Error_Description {
You shouldn’t need the SerializedName annotations.
These are for when the keys are not valid Java variable names, e.g. "abc-123", or "123abc".
Here is an example.