jackson转换json大写_Jackson - Java bean到JSON字符串:大写变量在JSON中转换为小写...

bd96500e110b49cbb3cd949968f18be7.png

I am converting Java bean to JSON string using writeValueAsString method of ObjectMapper where uppercase variables from Java bean is being changed to lowercase in JSON string.

Jackson 2.7.4 version implemented.

Base bean sample -

public class BaseBean {

private static final long serialVersionUID = 3947489072259877540L;

private int _iXId;

private String _sPNR;

private ArrayList _alMinPriced = new ArrayList();

public int getXId() {

return _iXId;

}

public void setXId(int id) {

_iXId = id;

}

public String getPNRNumber() {

return _sPNR;

}

public void setPNRNumber(String _spnr) {

_sPNR = _spnr;

}

public ArrayList getMinPriced() {

return _alMinPriced;

}

public void setMinPriced(ArrayList minPriced) {

_alMinPriced = minPriced;

}

public void setMinPriced(TermBean bnTerm) {

_alMinPriced.add(bnTerm);

}

}

Earlier, we were using net.sf.json.JSON & JSONSerializer for Java bean to JSON conversion. And generated JSON string was having similar naming as what we are having Java bean. Due to performance issue, I want to change this & implement Jackson.

Restrictions : we can't change Java bean naming convention as these beans are from older project and there is little scope to change the variable names in bean and even adding json properties in each bean.

I have tried below code but that didn't worked

ObjectMapper mapper = new ObjectMapper();

mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

mapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);

Also, I have tried customized PropertyNamingStrategy but not clear on this.

Edited :

net.sf.json.JSON generated JSON string as mentioned below for above bean :

{"XId":11,"PNRNumber":"123456789","minPriced":[{"name":"JSON"},{"name":"simple"}]}

Jackson generated JSON string as mentioned below for above bean :

{"xid":11,"pnrnumber":"123456789","minPriced":[{"name":"JSON"},{"name":"Jackson"}]}

As you can see that "XId" converted to "xid" in jackson and "PNRNumber" converted to "pnrnumber" in jackson.

Is there any configuration changes available in Jackson to avoid such modification.

OR How to handle such scenario.

解决方案

Following jars have been used:

jackson-core-2.7.4.jar

jackson-annotations-2.7.4.jar

jackson-databind-2.7.4.jar

Step 1: Please write following Mixin as follows:

import java.util.ArrayList;

import com.fasterxml.jackson.annotation.JsonProperty;

public abstract class MixIn {

@JsonProperty("PNRNumber")

abstract String getPNRNumber();

@JsonProperty("XId")

abstract int getXId();

@JsonProperty("minPriced")

abstract ArrayList getMinPriced();

}

Step 2: Please write your Module as follows:-

import com.fasterxml.jackson.databind.module.SimpleModule;

public class MyModule extends SimpleModule{

public MyModule() {

super("ModuleName");

}

@Override

public void setupModule(SetupContext context){

context.setMixInAnnotations(BaseBean.class, MixIn.class);

}

}

Step 3: Now its time to get json String as follows:

TermBean bean1=new TermBean("JSON");

TermBean bean2=new TermBean("simple");

ArrayList list=new ArrayList();

list.add(bean1);

list.add(bean2);

BaseBean bb=new BaseBean();

bb.setXId(11);

bb.setPNRNumber("123456789");

bb.setMinPriced(list);

ObjectMapper mapper = new ObjectMapper();

Module myModule = new MyModule();

mapper.registerModule(myModule);

String jsonInString = mapper.writeValueAsString(bb);

System.out.printf( "JSON: %s", jsonInString );

Output:

JSON:

{"XId":11,"PNRNumber":"123456789","minPriced":[{"name":"JSON"},{"name":"simple"}]}

Hope this helps.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值