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;
GitHub 加速计划 / fastj / fastjson
25.69 K
6.51 K
下载
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
最近提交(Master分支:3 个月前 )
c942c834 - 1 年前
5bc4709b - 1 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐