Delete JSON Descriptor in Java

I have the following listing in Java on Android, and I would like to be able to deserialize the integer in the input string / JSON object into this Enum type. I get hits on Jackson and GSON, but nothing on the JSON.org package that I use.

Is there an easy way to do this or do I need to change the JSON decoder? Thanks.

public enum ValueEnum { ONE(1), TWO(2), THREE(3); private int value; private ValueEnum(int value) { this.value = value; } public int getValue() { return value; } } 
+7
source share
1 answer

ValueEnum.values() will return an array of ValueEnum [], then you can iterate through the array and check the Value

 public static ValueEnum valueOf(int value) { ValueEnum[] valueEnums = ValueEnum.values(); for (ValueEnum valueEnum : valueEnums) { if (valueEnum.getValue() == value) { return valueEnum; } } return DEFAULT; } 
+7
source

All Articles