JAVA的驼峰和下划线互转帮助类

实体类:

 1 import java.io.Serializable;
 2 import lombok.AllArgsConstructor;
 3 import lombok.Data;
 4 import lombok.NoArgsConstructor;
 5 
 6 @Data
 7 @AllArgsConstructor
 8 @NoArgsConstructor
 9 public class User implements Serializable {
10     /**
11      * 
12      */
13     private static final long serialVersionUID = -329066647199569031L;
14     
15     private String userName;
16     
17     private String orderNo;
18 }

帮助类:

 1 import java.io.IOException;
 2 
 3 import com.fasterxml.jackson.annotation.JsonInclude.Include;
 4 import com.fasterxml.jackson.core.JsonProcessingException;
 5 import com.fasterxml.jackson.databind.ObjectMapper;
 6 import com.fasterxml.jackson.databind.PropertyNamingStrategy;
 7 
 8 /**
 9  * JSON的驼峰和下划线互转帮助类
10  * 
11  * @author yangzhilong
12  *
13  */
14 public class JsonUtils {
15 
16     /**
17      * 将对象的大写转换为下划线加小写,例如:userName-->user_name
18      * 
19      * @param object
20      * @return
21      * @throws JsonProcessingException
22      */
23     public static String toUnderlineJSONString(Object object) throws JsonProcessingException{
24         ObjectMapper mapper = new ObjectMapper();
25         mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
26         mapper.setSerializationInclusion(Include.NON_NULL);      
27         String reqJson = mapper.writeValueAsString(object);
28         return reqJson;
29     }
30 
31     /**
32      * 将下划线转换为驼峰的形式,例如:user_name-->userName
33      * 
34      * @param json
35      * @param clazz
36      * @return
37      * @throws IOException
38      */
39     public static <T> T toSnakeObject(String json, Class<T> clazz) throws IOException{
40         ObjectMapper mapper = new ObjectMapper();
   // mapper的configure方法可以设置多种配置(例如:多字段 少字段的处理)
       //mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
41 mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); 42 T reqJson = mapper.readValue(json, clazz); 43 return reqJson; 44 } 45 }

单元测试类:

 1 import java.io.IOException;
 2 
 3 import org.junit.Test;
 4 
 5 import com.alibaba.fastjson.JSONObject;
 6 import com.fasterxml.jackson.core.JsonProcessingException;
 7 
 8 public class JsonTest {
 9     
10     @Test
11     public void testToUnderlineJSONString(){
12         User user = new User("张三", "1111111");
13         try {
14             String json = JsonUtils.toUnderlineJSONString(user);
15             System.out.println(json);
16         } catch (JsonProcessingException e) {
17             e.printStackTrace();
18         }
19     }
20     
21     @Test
22     public void testToSnakeObject(){
23         String json = "{\"user_name\":\"张三\",\"order_no\":\"1111111\"}";
24         try {
25             User user = JsonUtils.toSnakeObject(json, User.class);
26             System.out.println(JSONObject.toJSONString(user));
27         } catch (IOException e) {
28             e.printStackTrace();
29         }
30     }
31 }

测试结果:

1 {"user_name":"张三","order_no":"1111111"}
2 
3 {"orderNo":"1111111","userName":"张三"}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值