java中如何使用Jackson将json串中带下划线的属性转换为驼峰属性呢?
下文笔者讲述json下划线属性转换为驼峰属性的方法分享,如下所示
实现思路: 只需设置相应的转换属性即可 如: mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);例:json中的下划线属性转换为驼峰属性
public class JacksonTest { public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); String json = "{\"user_name\":\"java265\",\"id_number\":\"879877823237877\"}"; mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); O o = (O) mapper.readValue(json, O.class); System.out.println(o.getIdNumber()); } } class O implements Serializable{ private static final long serialVersionUID = -3004824622398622080L; private String userName ; private String idNumber ; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getIdNumber() { return idNumber; } public void setIdNumber(String idNumber) { this.idNumber = idNumber; } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。