fastJson、Jackson对象转json串不使用驼峰方法和json转对象

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44778952/article/details/90207789

fastJson、Jackson对象转json串不使用驼峰方法

fastJson可以使用JSONField注解,jackson可以使用JsonProperty注解,两者结合起来,就可以同时支持了

代码示例:

package cn.weltman.dp.integration.common.facade.vo.resp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.SerializationConfig;
import lombok.Data;

import java.io.Serializable;


public class OrderRspTest implements Serializable {

    @JSONField(name = "MASTER_ORDER_NO")
    private String MASTER_ORDER_NO;
    @JSONField(name = "SUB_ORDER_NO")
    private String SUB_ORDER_NO;
    @JSONField(name = "SUB_ORDER_NO1")
    private String SUB_ORDER_NO1;

    @JsonProperty("MASTER_ORDER_NO")
    public String getMASTER_ORDER_NO() {
        return MASTER_ORDER_NO;
    }
    @JsonProperty("MASTER_ORDER_NO")
    public void setMASTER_ORDER_NO(String MASTER_ORDER_NO) {
        this.MASTER_ORDER_NO = MASTER_ORDER_NO;
    }
    @JsonProperty("SUB_ORDER_NO")
    public String getSUB_ORDER_NO() {
        return SUB_ORDER_NO;
    }
    @JsonProperty("SUB_ORDER_NO")
    public void setSUB_ORDER_NO(String SUB_ORDER_NO) {
        this.SUB_ORDER_NO = SUB_ORDER_NO;
    }
    @JsonProperty("SUB_ORDER_NO1")
    public String getSUB_ORDER_NO1() {
        return SUB_ORDER_NO1;
    }
    @JsonProperty("SUB_ORDER_NO1")
    public void setSUB_ORDER_NO1(String SUB_ORDER_NO1) {
        this.SUB_ORDER_NO1 = SUB_ORDER_NO1;
    }

    public static void main(String[] args) throws JsonProcessingException {
        MESRspTest mes=new MESRspTest();
        mes.setMASTER_ORDER_NO("a");
        mes.setSUB_ORDER_NO("b");
        mes.setSUB_ORDER_NO1("c");
        ObjectMapper mapper = new ObjectMapper();
//        mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, Boolean.TRUE);
        String json = mapper.writeValueAsString(mes);
        System.out.println(json);
        json=JSON.toJSONString(mes);
        System.out.println(json);
    }
}

json转对象

jackson 只需要在对象的对应属性上加上JsonProperty注解就可以接收到数据,

@JsonProperty("gJAVA")
private String buzei;

猜你喜欢

转载自blog.csdn.net/weixin_44778952/article/details/90207789