简体   繁体   English

Jackson-Java bean到JSON字符串:大写变量在JSON中转换为小写

[英]Jackson - Java bean to JSON string : uppercase variable converted into lowercase in JSON

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. 我正在使用ObjectMapper的writeValueAsString方法将Java bean转换为JSON字符串,其中Java bean中的大写变量被更改为JSON字符串中的小写字母。 Jackson 2.7.4 version implemented. 实施了Jackson 2.7.4版本。 Base bean sample - 基础豆样品-

public class BaseBean {

private static final long serialVersionUID = 3947489072259877540L;

private int _iXId;
private String _sPNR;
private ArrayList _alMinPriced = new ArrayList<TermBean>();

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. 之前,我们使用net.sf.json.JSON和JSONSerializer将Java bean转换为JSON。 And generated JSON string was having similar naming as what we are having Java bean. 生成的JSON字符串与我们使用的Java bean具有相似的命名。 Due to performance issue, I want to change this & implement Jackson. 由于性能问题,我想对此进行更改并实施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. 限制:我们无法更改Java bean的命名约定,因为这些bean来自较旧的项目,并且几乎没有范围来更改bean中的变量名,甚至在每个bean中添加json属性。

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. 另外,我尝试了自定义的PropertyNamingStrategy,但不清楚。

Edited : 编辑:

net.sf.json.JSON generated JSON string as mentioned below for above bean : net.sf.json.JSON生成了JSON字符串,如下所述:

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

Jackson generated JSON string as mentioned below for above bean : Jackson为上述bean生成了JSON字符串,如下所述:

{"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. 如您所见,在jackson中,“ XId”转换为“ xid”,在jackson中,“ PNRNumber”转换为“ pnrnumber”。

Is there any configuration changes available in Jackson to avoid such modification. Jackson中是否有任何配置更改可用于避免此类更改。 OR How to handle such scenario. 或如何处理这种情况。

Following jars have been used: 使用了以下罐子:

  1. jackson-core-2.7.4.jar 杰克逊核心2.7.4.jar
  2. jackson-annotations-2.7.4.jar 杰克逊的注解 - 2.7.4.jar
  3. jackson-databind-2.7.4.jar 杰克逊 - 数据绑定 - 2.7.4.jar

Step 1: Please write following Mixin as follows: 步骤1:请按以下方式编写以下Mixin:

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:- 步骤2:请按照以下步骤编写模块:

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: 步骤3:现在是时候获取json String了,如下所示:

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"}]} JSON:{“ XId”:11,“ PNRNumber”:“ 123456789”,“ minPriced”:[{“ name”:“ JSON”},{“ name”:“ simple”}]}}

Hope this helps. 希望这可以帮助。

Add Json Property with required keycase. 添加具有必需键区的Json属性。 Create variable with lowercase. 用小写字母创建变量。

public class BaseBean {

@JsonProperty("XId")
private int xId;
..
}

Hope this will help 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM