import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializeFilter; import com.alibaba.fastjson.serializer.SerializerFeature;
public class Util{ class Person{ private String userName; private Long userId; private Date createTime; } public void testUtil(){ Person person = new Person(); person.setUserName("yM"); person.setUserId("12"); person.setCreateTime(new Date()); List<Person> lists = new ArrayList(); lists.add(person );
SerializeConfig config = new SerializeConfig(); config.setPropertyNamingStrategy(PropertyNamingStrategy.SnakeCase);
//使用alibaba 的包 //config 是设置峰格式key转换成下滑线 //DEFAULT_GENERATE_FEATURE 时间格式化 (每次在json转换后就是时间戳了) String st = JSON.toJSONString(lists , config ,(SerializeFilter[])null,"yyyy-MM-dd HH:mm:ss",DEFAULT_GENERATE_FEATURE,SerializerFeature.WriteDateUseDateFormat);
System.out.pringln(st): //结果:[{"user_name":"yM","user_id":12,"create_time":"2023:04:27 11:27:00"}] } }
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.83</version> </dependency>