fastjson 序列化时指定json的key值
原创
©著作权归作者所有:来自51CTO博客作者gblfy的原创作品,请联系作者获取转载授权,否则将追究法律责任
json字符串中的key是下划线例如:foot_dist
,而实际实体类中的属性是footDist
,添加 @JSONField(name = "foot_dist")
即可
package com.gblfy;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Segments {
@JSONField(name = "line_name")
private String lineName;
@JSONField(name = "foot_dist")
private String footDist;
@JSONField(name = "stat_xys")
private String statXys;
@JSONField(name = "stats")
private String stats;
@JSONField(name = "end_stat")
private String endStat;
@JSONField(name = "line_xys")
private String lineXys;
@JSONField(name = "line_dist")
private String lineDist;
@JSONField(name = "start_stat")
private String startStat;
}
(String[] args) {
Segments build = new Segments().builder()
.footDist("123")
.startStat("456")
.stats("2").build();
System.out.println("sss"+JSON.toJSONString(build));
}