The problem is the JSON specification and what you are doing.
The JSON specification specifies only one numeric type, which can include a decimal point and a fractional part:
2.4. The numbers
The representation of numbers is similar to the representation used in most cases by programming languages. The number contains an integer component, which may have a prefix with an optional minus sign, followed by part of the fraction and / or exponential part.
JSON parsers are left to decide for themselves what to do with this number type when parsing / matching JSON.
In your case, your Data class has num defined as Object . This gives Gson no clue as to which specific type of Java type you need to display a numeric JSON type. Gson authors decided to use Double when this happens, regardless of whether the number in JSON includes decimal + fraction or not.
This really makes sense when you consider that an integer can be expressed as a double, but not vice versa. Using one type, not parsing a number and deciding whether int or double supports consistent behavior.
It is not clear why you are not using Integer (or int ) for num in your Data object, if that is what you expect / need. You declare that you need to send to Integer "later", which means the only thing that the object can be in the first place - Integer ; any other casting attempt will fail.
source share