我在Snake_Case中使用带有属性的json字符串为" Country_Code":" US"," Group_Member_Code":null
我必须将其映射到具有camelCase属性的对象
class Output{
@JsonProperty("countryCode")
private String countryCode;
@JsonProperty("groupMemberCode")
private String groupMemberCode;
}
要按照其他线程的建议执行此操作,我将自定义PropertyNamingStratergy创建为
public class CustomCase extends PropertyNamingStrategy.PropertyNamingStrategyBase {
public CustomCase() {
}
public String translate(String input) {
if (input == null) {
return input;
} else {
char[] arr = input.toCharArray();
StringBuilder result = new StringBuilder();
result.append(Character.toLowerCase(arr[0]));
for (int i = 1; i < arr.length; i++) {
if (arr[i] == '_') {
result.append(Character.toUpperCase(arr[i + 1]));
i++;
} else {
result.append(arr[i]);
}
}
return result.toString();
}
}
}
我正在将其设置为我正在反序列化的类
JAXBContext jaxbContext = JAXBContext.newInstance(InstinctFraudCheckXMLStringResponse.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
InstinctFraudCheckXMLStringResponse fraudResponse = (InstinctFraudCheckXMLStringResponse)
unmarshaller.unmarshal(new ByteArrayInputStream(response.getBytes()));
XmlMapper xmlMapper = new XmlMapper();
JsonNode node = xmlMapper.readTree(fraudResponse.getInstinctFraudCheckXMLStringResult().getBytes());
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
mapper.setPropertyNamingStrategy(new CustomCase());
fraudDecision = mapper.writeValueAsString(node);
FraudResponse fraudResponse1 = mapper.readValue(fraudDecision,FraudResponse.class);
System.out.println(fraudResponse1);
但似乎mu NamingStratergy无法正常工作,它无法将snake_case转换为驼峰并将其映射到Object,因为我的FraudResponse总是来NUll。
请帮我找出为什么NamingStratgery无法正常工作。
答案 0 :(得分:0)
首先,按如下所示更改Output类
class Output {
// Value from JsonProperty annotation removed. Will handle in naming strategy
@JsonProperty
private String countryCode;
@JsonProperty
private String groupMemberCode;
}
然后为您的ObjectMapper
//For jackson < 2.7
objectMapper.setPropertyNamingStrategy(
PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
//For jackson >= 2.7
objectMapper.setPropertyNamingStrategy(
PropertyNamingStrategy.PropertyNamingStrategy.SNAKE_CASE);
示例强>
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
Output output = new Output();
output.countryCode="US";
output.groupMemberCode = "ABC";
System.out.println(objectMapper.writeValueAsString(output));
<强>打印强>
{"country_code":"US","group_member_code":"ABC"}
答案 1 :(得分:0)
我认为你可以试试这个解决方案(在我的情况下工作):
删除@JsonProperty
注释。
使用objectMapper指定策略:
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
工作示例:
class Output {
private String countryCode;
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
}
String data = "{\"Country_Code\":\"US\"}";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
mapper.readValue(data, Output.class);