I get json response from server. And I am parsing it with the GSON library.
The key in json is an integer value. How is it possible, without changing the server response (this is the external interface of the server, there is no influence on us) in order to pass an integer value to an enumeration?
Thank.
UPD:
Answer json-Response. NOTE: we cannot change it.
"testObject":{
"id":123,
"type":42
}
Listing:
public enum ObjectTypeEnum
{
UNKNOWN_TYPE(0),
SIMPLE_TYPE(11),
COMPLEX_TYPE(42);
private int value;
private ObjectTypeEnum(int value)
{
this.value = value;
}
public static ObjectTypeEnum findByAbbr(int value)
{
for (ObjectTypeEnum currEnum : ObjectTypeEnum.values())
{
if (currEnum.value == value)
{
return currEnum;
}
}
return null;
}
public int getValue()
{
return value;
}
}
And the class of the object
public class TestObject
{
publuc int id;
public ObjectTypeEnum type;
}
source
share