Skip to content
Advertisement

Parse JSON Data using wrapper class, where String’s object/key name is numeric

I am trying to parse a special JSON data using Wrapper class, special means a JSON which have numeric keys as string like below :

{
"date":"2018-11-01",
"hours":"01-Nov 08:00",
"1011":"837.7500",
"1022":"99.92596979567664",
"1010":"3.198083",
"1021":"5",
"1019":"1171.000",
"1018":"3.578371",
"1017":"30.46989",
"1016":"0.0001931423",
"1015":"6749",
"1014":"0.161805",
"1013":"0.001678397",
"1012":"1.406077"
}

I know how to parse JSON data using wrapper class but this case is special because wrapper class’s object name not accepting the numeric as String.

Wrapper/POJO Class

enter image description here

I don’t want to go with JSON object based parsing. Anyone have any idea about it?

Advertisement

Answer

The Gson library has SerializedName functionality in which it parses the corresponding value of the key defined in SerializeName’s parameter. Things will be tougher if your key is a pure integer since Java disallows it as variable name, in this case SerializeName will save you from that headache and it makes your code way more maintainable.

Example usage :

@SerializedName("1011") double lat;
@SerializedName("1022") double lng;
Advertisement