Json字符串转换处理html编码格式,= u003d 处理
代码语言:javascript复制import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@JsonAutoDetect(fieldVisibility= JsonAutoDetect.Visibility.ANY, getterVisibility=JsonAutoDetect.Visibility.NONE)
public class JsonReqVo {
@Expose
@SerializedName("Username") //gson
@JSONField(name= "Username") //fastjson
private String username;
@Expose
@JsonProperty("Status")
private String status; //jackson
@Expose
private String school;
@Expose
private String password;
@Expose
private String address;
@JsonIgnore //可以直接放在field上面表示要忽略的filed //jackson
@Expose(serialize = false) //gson
@JSONField(serialize = false) //fastjson
private String apikey;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getApikey() {
return apikey;
}
public void setApikey(String apikey) {
this.apikey = apikey;
}
}
//测试类
import com.alibaba.fastjson.JSON;
import com.example.core.mydemo.json.vo.JsonReqVo;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
* Gson={"Username":"testeru003du003du003d","password":"#nn"}
* FASTJSON={"Username":"tester===","password":"#nn"}
* JACKSON={"username":"tester===","password":"#nn"}
*/
public class Json2Test {
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
public static void main(String[] args) {
JsonReqVo req = new JsonReqVo();
req.setUsername("tester===");
req.setPassword("#nn");
GsonBuilder builder = new GsonBuilder();
builder.excludeFieldsWithoutExposeAnnotation();
//设置属性:disableHtmlEscaping
Gson gson = builder.disableHtmlEscaping().create(); //Gson={"Username":"tester===","password":"#nn"}
System.out.println("Gson=" gson.toJson(req)) ;
System.out.println("FASTJSON=" JSON.toJSONString(req)) ;