ObjectMapper 中是否有任何方法可以将 json 字符串与 Enum 匹配
Is there any method in ObjectMapper to match json string with Enum
我有一个枚举
public enum status{
YES,
NO
}
json字符串的输入是"Yes"或"No",ObjectMapper中有没有什么方法可以匹配status.YES和"Yes",status.NO 与 "No".
我不想更改枚举,因为在我以前的系统中,人们一直在使用枚举,我不想给其他人造成问题
您可以随时重新定义它:
public enum Status {
YES("Yes"),
NO("No");
private final String status;
private Status(final String status) {
this.status = status;
}
public String value() {
return this.status;
}
}
然后使用这样的东西:Status.YES.value()
;
您可以使用所有 Java 枚举中可用的 toString()
方法:
http://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html
并在返回的 String
上调用 compareToIgnoreCase
方法将其与输入进行比较:
http://www.tutorialspoint.com/java/java_string_comparetoignorecase.htm
或者您可以在输入 String
上调用 toUpperCase
,然后对它们进行比较:
http://www.tutorialspoint.com/java/java_string_touppercase.htm
最后可以使用前面提到的toString
,把除第一个字母以外的字母全部小写:
String YesString = enumWithYESValue.toString().substring(0, 1) + enumWithYESValue.toString().substring(1).toLowerCase();
基于:How to capitalize the first letter of a String in Java?
我有一个枚举
public enum status{
YES,
NO
}
json字符串的输入是"Yes"或"No",ObjectMapper中有没有什么方法可以匹配status.YES和"Yes",status.NO 与 "No".
我不想更改枚举,因为在我以前的系统中,人们一直在使用枚举,我不想给其他人造成问题
您可以随时重新定义它:
public enum Status {
YES("Yes"),
NO("No");
private final String status;
private Status(final String status) {
this.status = status;
}
public String value() {
return this.status;
}
}
然后使用这样的东西:Status.YES.value()
;
您可以使用所有 Java 枚举中可用的 toString()
方法:
http://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html
并在返回的 String
上调用 compareToIgnoreCase
方法将其与输入进行比较:
http://www.tutorialspoint.com/java/java_string_comparetoignorecase.htm
或者您可以在输入 String
上调用 toUpperCase
,然后对它们进行比较:
http://www.tutorialspoint.com/java/java_string_touppercase.htm
最后可以使用前面提到的toString
,把除第一个字母以外的字母全部小写:
String YesString = enumWithYESValue.toString().substring(0, 1) + enumWithYESValue.toString().substring(1).toLowerCase();
基于:How to capitalize the first letter of a String in Java?