use*_*618 9 enums json deserialization
我正在尝试反序列化一个 json 字符串,该字符串将枚举作为其值之一。
枚举结构如下
ENUM STATUS
{
    ACTIVE(0), INACTIVE(1), EXPIRED(3)// Note here that 2 is not used due to some reasons
}
int status = 0;  
public static Status getNameByValue(final int value) {
        for (final Status s: Status.values()) {
            if (s.status== value) {
                return s;
            }
        }
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)
当我尝试读取一个 json 字符串时,该字符串将其作为其值之一,如下所示
{"name":"Raj","status": 3}
Run Code Online (Sandbox Code Playgroud)
我有以下例外。
number value (3): index value outside legal index range [0..2]
 at [Source: org.apache.catalina.connector.CoyoteInputStream@9e89a21; line: 1, column: 28] (through reference chain: 
Run Code Online (Sandbox Code Playgroud)
请在这方面帮助我
getNameByValue只需用@JsonCreator, 对我的作品进行注释即可
ENUM STATUS
{
    ACTIVE(0), INACTIVE(1), EXPIRED(3) // Note here that 2 is not used due to some reasons
}
int status = 0;  
@JsonCreator
public static Status getNameByValue(final int value) {
        for (final Status s: Status.values()) {
            if (s.status== value) {
                return s;
            }
        }
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           5618 次  |  
        
|   最近记录:  |