package com.fkit.json; import com.alibaba.fastjson.annotation.JSONField; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import net.sf.json.JSONObject; public class UserInfo { //@JsonProperty("ID") @JsonIgnore private Integer crmStatus; @JsonProperty( "C_NAME") private String C_NAME; @JsonProperty( "C_MOBILE") private String C_MOBILE; @JsonProperty( "C_URL") private String C_URL; public Integer getCrmStatus() { return crmStatus; } public void setCrmStatus(Integer crmStatus) { this.crmStatus = crmStatus; } @JsonIgnore public String getC_NAME() { return C_NAME; } public void setC_NAME(String C_NAME) { this.C_NAME = C_NAME; } @JsonIgnore public String getC_MOBILE() { return C_MOBILE; } public void setC_MOBILE(String C_MOBILE) { this.C_MOBILE = C_MOBILE; } @JsonIgnore public String getC_URL() { return C_URL; } public void setC_URL(String C_URL) { this.C_URL = C_URL; } @Override public String toString() { return JSONObject.fromObject(this).toString(); }
public static void main(String[] args) throws JsonProcessingException { UserInfo userInfo = new UserInfo(); userInfo.setC_NAME("故事"); System.out.println(new ObjectMapper().writeValueAsString(userInfo)); }}
要首字母大写,需要以下操作:
1、maven添加:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.3</version> </dependency>
2、属性上加 :
@JsonProperty( "C_NAME") 3、get方法上加:
@JsonIgnore
Jackson框架是基于
Java平台的一套数据处理工具,被称为“最好的
Java Json解析器”。
Jackson框架包含了3个核心库:streaming,databind,annotations.Jackson还包含了其它数据处理类库。
Jackson版本: 1.x的类库中,包命名以:org.codehaus.jackson.xxx开头,而2.x类库中包命令:com.fastxml.jackson.xxx开头。
推荐学些链接:https://blog.csdn.net/xiong9999/article/details/53781695