Vinod Kumar
Vinod Kumar

Reputation: 147

gson custom convert json key to string

My server is returning json key-value pair like

{
    "my-name":"name"
}

I am using retrofit lib. So that gson is converting this to java object. So I created java object like below

public class Example{
    public String myname;  // cannot have my-name variable
}

response is giving me is "myname=null". Because variable in json is my-name but I cannot have same variable in java class. How to have same variable name like json in java?

Upvotes: 3

Views: 1811

Answers (1)

Ezzored
Ezzored

Reputation: 925

You can add the @SerializedName("my-name") annotation to your POJO class like this:

public class Example{
   @SerializedName("my-name")
   public String myname;  // cannot have my-name variable
}

YOu can read more about this in the GSON documentation: https://sites.google.com/site/gson/gson-user-guide

Upvotes: 11

Related Questions