实体类:

 import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = -329066647199569031L; private String userName; private String orderNo;
}

帮助类:

 import java.io.IOException;

 import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy; /**
* JSON的驼峰和下划线互转帮助类
*
* @author yangzhilong
*
*/
public class JsonUtils { /**
* 将对象的大写转换为下划线加小写,例如:userName-->user_name
*
* @param object
* @return
* @throws JsonProcessingException
*/
public static String toUnderlineJSONString(Object object) throws JsonProcessingException{
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
mapper.setSerializationInclusion(Include.NON_NULL);
String reqJson = mapper.writeValueAsString(object);
return reqJson;
} /**
* 将下划线转换为驼峰的形式,例如:user_name-->userName
*
* @param json
* @param clazz
* @return
* @throws IOException
*/
public static <T> T toSnakeObject(String json, Class<T> clazz) throws IOException{
ObjectMapper mapper = new ObjectMapper();
   // mapper的configure方法可以设置多种配置(例如:多字段 少字段的处理)
       //mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
T reqJson = mapper.readValue(json, clazz);
return reqJson;
}
}

单元测试类:

 import java.io.IOException;

 import org.junit.Test;

 import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException; public class JsonTest { @Test
public void testToUnderlineJSONString(){
User user = new User("张三", "1111111");
try {
String json = JsonUtils.toUnderlineJSONString(user);
System.out.println(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
} @Test
public void testToSnakeObject(){
String json = "{\"user_name\":\"张三\",\"order_no\":\"1111111\"}";
try {
User user = JsonUtils.toSnakeObject(json, User.class);
System.out.println(JSONObject.toJSONString(user));
} catch (IOException e) {
e.printStackTrace();
}
}
}

测试结果:

 {"user_name":"张三","order_no":"1111111"}

 {"orderNo":"1111111","userName":"张三"}

JAVA的驼峰和下划线互转帮助类的更多相关文章

  1. jackson 常用注解,比如忽略某些属性,驼峰和下划线互转

    一般情况下使用JSON只使用了java对象与字符串的转换,但是,开发APP时候,我们经常使用实体类来做转换:这样,就需要用到注解: Jackson默认是针对get方法来生成JSON字符串的,可以使用注 ...

  2. JS驼峰与下划线互转

    1.下划线转驼峰 function underlineToHump(s){ var a = s.split("_"); var result = a[0]; for(var i=1 ...

  3. 【SCALA】2、驼峰,下划线互转

    1.刚开始写scala,发现确实还是很不熟悉,api以及语法的使用都不是很简洁,这写出来跟java也没差多少... 献丑了 package spark /** * @ProjectName: cutt ...

  4. Java实现递归将嵌套Map里的字段名由驼峰转为下划线

    摘要: 使用Java语言递归地将Map里的字段名由驼峰转下划线.通过此例可以学习如何递归地解析任意嵌套的List-Map容器结构. 难度:初级 概述 在进行多语言混合编程时,由于编程规范的不同, 有时 ...

  5. java驼峰法和下划线法字符串的相互转换

    java驼峰法和下划线法字符串的相互转换 1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class ...

  6. 递归将Map里的字段名由驼峰转为下划线

    导航 定位 概述 算法设计 递归技巧 代码实现 定位 本文适合于想要使用Java递归地将Map里的Key字段名从驼峰转为下划线,或者想了解如何处理任意递归的Map结构的筒鞋. 概述 在进行多语言混合编 ...

  7. $聊一聊"驼峰"和"下划线"——Python re.sub函数详细用法实例讲解

    日常写代码时候会遇到一些字符串替换的操作,比如把一大堆"驼峰"形式的字符串批量转换成下划线形式."驼峰"形式的变量命名风格在Java中很常见,而下划线形式的变量 ...

  8. js字符串驼峰和下划线互相转换

    // 下划线转换驼峰 function toHump(name) { return name.replace(/\_(\w)/g, function(all, letter){ return lett ...

  9. python面向对象双下划线方法与元类

    目录 双下划线方法(__) 元类简介 产生类的两种表现形式 元类的基本使用 元类进阶操作 __new__方法 双下划线方法(__) 面向对象中的双下方法也有一些人称之为是魔法方法,有些双下方法不需要刻 ...

随机推荐

  1. 使用Logstash创建ES映射模版并进行数据默认的动态映射规则

    本文配置为 ELK 即(Elasticsearch.Logstash.Kibana)5.5.1. Elasticsearch 能够自动检测字段的类型并进行映射,例如引号内的字段映射为 String,不 ...

  2. 关于 as 播放器的记录

    一:文件结构 1:代码 2:编译后   二:IDE展示区 1处还有6个层,2处为代码和设计文件,3处是主类. 资源文件的位置如下:   三:数据交互 AS中代码: JS中代码: 更多需要注意的地方在这 ...

  3. Dockerfile 指令汇总及解析

        原文地址:http://www.maoyupeng.com/dockerfile-command-introduction.html 什么是Dockerfile Dockerfile是由一系列 ...

  4. State of Serverless

    Some quick thoughts from Serverlessconf, Austin in April 2017 I wanted to take a bit of time to writ ...

  5. MySQL C API的一个让我头疼的问题,获得一行记录中包括NULL

    遇到过几次错误,通过gdb来查看错误对战,发现错误居然是atoi调用出错,除非atoi(NULL) 才会报这种错误.说明 row[0]==NULL. (gdb) bt #0 0x00007f82c66 ...

  6. Swift: Swift中Selector的变化

    Swift中Selector变化 2.2 之前,使用字符串作为方法名称 //无参数 btn.addTarget(self, action: Selector("buttonPress&quo ...

  7. bat 批处理切换到当前脚本所在文件夹

    bat 批处理切换到当前脚本所在文件夹   切换到当前脚本所在的文件夹 ? 1 cd  %~dp0 另外附上一些bat基本内容 —————————————————————————————— 批处理常用 ...

  8. DOS批处理中%cd%和%~dp0的异同分析

    在DOS的批处理中,有时候需要知道当前的路径.在DOS中,有两个环境变量可以跟当前路径有关,一个是%cd%, 一个是%~dp0. 这两个变量的用法和代表的内容是不同的. 1. %cd% 可以用在批处理 ...

  9. 中文分词库及NLP介绍,jieba,gensim的一些介绍

    六款中文分词软件介绍: https://blog.csdn.net/u010883226/article/details/80731583 里面有jieba, pyltp什么的.另外下面这个博客有不少 ...

  10. 使用word2vec训练中文词向量

    https://www.jianshu.com/p/87798bccee48 一.文本处理流程 通常我们文本处理流程如下: 1 对文本数据进行预处理:数据预处理,包括简繁体转换,去除xml符号,将单词 ...