简体   繁体   English

将对象转换为JSON控制字段名称大写首字母

[英]Converting object to json control fields name upper case first letter

Object: 宾语:

@XmlRootElement
public class AccountSyncResponse 
{
        private String Result;
        private String Value;

        public AccountSyncResponse() {}            

        public String getResult() {return Result;}
        public void setResult(String Result) {this.Result = Result;}
        public String getValue() {return Value;}
        public void setValue(String Value) {this.Value = Value;}        
}

Rest Web Service: 其余Web服务:

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public AccountSyncResponse excute(AccountSyncRequest ASReq) 
    {        
       AccountSyncResponse ASRes = new AccountSyncResponse();
       return ASRes;    
    }

The result is {"result":"Create","value":"123456"} 结果是{"result":"Create","value":"123456"}

I need it the first letter of fields name to be in upper case {"Result":"Create","Value":"123456"} 我需要字段名称的首字母大写{"Result":"Create","Value":"123456"}

How can I control fields name in the resulting json string ? 如何控制结果json字符串中的字段名称?

You can use @XmlElement as show below: 您可以使用@XmlElement ,如下所示:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class AccountSyncResponse  {

    @XmlElement(name = "Result")
    private String result;

    @XmlElement(name = "Value")
    private String value;

    // Default constructor, getters and setters
}

Alternatively you can annotate the getters with @XmlElement (then the @XmlAccessorType annotation is not required). 或者,您可以使用@XmlElement注释吸气剂(然后@XmlAccessorType注释)。


Alternatively to JAXB annotation, you might want to consider Jackson. 除了JAXB注释,您可能还需要考虑Jackson。 It's a popular JSON parser for Java that can be used with Jersey . 这是可用于Jersey的流行的Java JSON解析器。 Then you can use @JsonProperty instead (however Jackson can also work with JAXB annotations). 然后,您可以改用@JsonProperty (但是Jackson也可以使用JAXB注释)。

With Jackson, depending on your needs, you can use a PropertyNamingStrategy such as PropertyNamingStrategy.UpperCamelCaseStrategy . 对于Jackson,根据您的需求,可以使用PropertyNamingStrategy例如PropertyNamingStrategy.UpperCamelCaseStrategy

暂无
暂无

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

相关问题 在Char中将CharSequence的第一个字母改为大写 - changing CharSequence first letter to upper case in android 从第一个大写字母拆分字符串 - Split the string from first Upper case letter 大写转换 - 将字符串中每个单词的第一个字母转换为大写 - Upper case conversion - convert the first letter of each word in the string to uppercase 杰克逊 - 将java对象转换为json - 需要所有关键键为大写 - Jackson - converting java object to json - Need all key keys to upper case 将起始字母改为大写字母 - change starting letter to upper case git将我的第一个包裹的字母从小写转换为大写 - git transform my first package's letter from lower case to upper case 如何在字符串中每个单词的第一个字母大写,同时在Java中删除最终的空格? - How to upper case every first letter of word in a string, meanwhile removing eventual whitespaces in Java? 需要在包含少量单词的字符串中,该单词的每个首字母都是大写的 - Need in String that contain few word that each first letter of the word will be upper case 将字符串的替代字符转换为大写。 字符串的第一个字母必须是大写 - convert the alternate characters of the string to upper case . The first letter of the string has to be Capital 我想检查字符串的空格,并想在空格后将第一个字符打印为大写字母 - I want to check the whitespace of a string and want to print the first character just after the whitespace to a upper case letter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM