Json字符串转换处理html编码格式,= \u003d 处理

Json字符串转换处理html编码格式,=  \u003d 处理

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":"tester\u003d\u003d\u003d","password":"#\n\n"}
 * FASTJSON={"Username":"tester===","password":"#\n\n"}
 * JACKSON={"username":"tester===","password":"#\n\n"}
 */
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("#\n\n");

        GsonBuilder builder = new GsonBuilder();
        builder.excludeFieldsWithoutExposeAnnotation();
        //设置属性:disableHtmlEscaping
        Gson gson = builder.disableHtmlEscaping().create();  //Gson={"Username":"tester===","password":"#\n\n"}

        System.out.println("Gson=" + gson.toJson(req)) ;

        System.out.println("FASTJSON=" + JSON.toJSONString(req)) ;

      
      
JSONObject jobj = JSONObject.parseObject(json);
System.out.println("jobj1=" + jobj.get("Username").toString());
System.out.println("jobj2=" + jobj.get("password").toString());


try { //过滤null属性 MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); System.out.println("JACKSON=" + MAPPER.writeValueAsString(req)); } catch (JsonProcessingException e) { e.printStackTrace(); } } }

 

posted on 2022-10-08 15:23  oktokeep  阅读(1951)  评论(0编辑  收藏  举报