何时使用 @JsonProperty 属性,它的用途是什么? [英] When is the @JsonProperty property used and what is it used for?

查看:31
本文介绍了何时使用 @JsonProperty 属性,它的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个 bean 'State' :

This bean 'State' :

public class State {

    private boolean isSet;

    @JsonProperty("isSet")
    public boolean isSet() {
        return isSet;
    }

    @JsonProperty("isSet")
    public void setSet(boolean isSet) {
        this.isSet = isSet;
    }

}

使用 ajax 'success' 回调通过网络发送:

is sent over the wire using the ajax ' success' callback :

        success : function(response) {  
            if(response.State.isSet){   
                alert('success called successfully)
            }

这里需要@JsonProperty 注释吗?使用它有什么好处?我想我可以在不引起任何副作用的情况下删除此注释.

Is the annotation @JsonProperty required here ? What is the advantage of using it ? I think I can remove this annotation without causing any side effects.

https://github.com/FasterXML/jackson 上阅读此注释-annotations/wiki/Jackson-Annotations 不知道什么时候需要用到这个?

Reading about this annotion on https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations I don't know when this is required to be used ?

推荐答案

这是一个很好的例子.我使用它来重命名变量,因为 JSON 来自 .Net 环境,其中属性以大写字母开头.

Here's a good example. I use it to rename the variable because the JSON is coming from a .Net environment where properties start with an upper-case letter.

public class Parameter {
  @JsonProperty("Name")
  public String name;
  @JsonProperty("Value")
  public String value; 
}

这正确解析到/从 JSON:

This correctly parses to/from the JSON:

"Parameter":{
  "Name":"Parameter-Name",
  "Value":"Parameter-Value"
}

这篇关于何时使用 @JsonProperty 属性,它的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆