Skip to content
Advertisement

Parse JSON Data using POJO, Where key name is numeric

I’m trying to parse a special JSON data using Wrapper class, special means a JSON which have numeric keys 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 POJO, But in this case java is not accepting the numeric as Keys.

Wrapper/POJO Class

enter image description here

I don’t want to go with JSON object based parsing. Is 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;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement