FastJson 支持配置的PropertyNamingStrategy四种策略
作者:互联网
摘要:
FastJson默认使用CamelCase,在1.2.15版本之后,FastJson支持配置PropertyNamingStrategy,支持四种策略: CamelCase、PascalCase、SnakeCase和KebabCase。
属性名策略说明:
CamelCase策略,Java对象属性:personId,序列化后属性:persionId
PascalCase策略,Java对象属性:personId,序列化后属性:PersonId
SnakeCase策略,Java对象属性:personId,序列化后属性:person_id
KebabCase策略,Java对象属性:personId,序列化后属性:person-id
属性名策略,案例代码:
package com.what21.fastjson.fj07; /** * 模型对象 */ public class Person { //ID public int personId; //名称 public String personName; // 邮箱 public String personEmail; // 手机号 public String personPhone; }
package com.what21.fastjson.fj07; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.PropertyNamingStrategy; import com.alibaba.fastjson.parser.ParserConfig; import com.alibaba.fastjson.serializer.SerializeConfig; public class FastJsonMain { /** * @param args */ public static void main(String[] args) { // 创建对象并赋值 Person person = new Person(); person.personId = 21; person.personName="小奋斗教程"; person.personEmail="1732482792@qq.com"; person.personPhone="156983444xx"; // 序列化配置对象 SerializeConfig config = new SerializeConfig(); config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;; // 序列化对象 String json = JSON.toJSONString(person,config); System.out.println("反序列 person json -> "); System.out.println(json); // 反序列化配置对象 ParserConfig parserConfig = new ParserConfig(); parserConfig.propertyNamingStrategy = PropertyNamingStrategy.CamelCase; // 反序列化对象 person = JSON.parseObject(json, Person.class, parserConfig); System.out.println("反序列化 内容 -> "); System.out.print(person.personId + " "); System.out.print(person.personName + " "); System.out.print(person.personEmail + " "); System.out.println(person.personPhone); } } 输出内容: 反序列 person json -> {"personEmail":"1732482792@qq.com","personId":21,"personName":"小奋斗教程","personPhone":"156983444xx"} 反序列化 内容 -> 21 小奋斗教程 1732482792@qq.com 156983444xx
标签:FastJson,personId,PropertyNamingStrategy,System,person,序列化,com,public,四种 来源: https://www.cnblogs.com/dand/p/10342597.html