@JsonProperty添加在属性上,并使用new ObjectMapper().writeValueAsString(object)转换为别名的JSON字符串
引入依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.3</version> <scope>compile</scope> </dependency>
创建实体类
import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; @Data public class DictVO { /** * 字典编号 */ @JsonProperty(value = "dict_type_id") private String dictTypeId; /** * 字典名称 */ @JsonProperty(value = "dict_type_name") private String dictTypeName; /** * 字典代码 */ @JsonProperty(value = "dict_type_code") private String dictTypeCode; }
编写测试类
public class demo { public static void main(String[] args) throws JsonProcessingException { DictVO dictVO = new DictVO(); dictVO.setDictTypeId("1745483801540300811"); dictVO.setDictTypeCode("1"); dictVO.setDictTypeName("激活"); String s = JSON.toJSONString(dictVO); System.out.println(s); DictVO dictVO1 = JSON.parseObject(s, DictVO.class); System.out.println(dictVO1); } }
结果:
{"dict_type_code":"1","dict_type_id":"1745483801540300811","dict_type_name":"激活"} DictVO(dictTypeId=1745483801540300811, dictTypeName=激活, dictTypeCode=1)
标签:JsonProperty,dictVO,java,String,dict,互转,DictVO,type From: https://www.cnblogs.com/zwh0910/p/17211793.html