使用Jackson更改JSON中的字段名称 [英] Change field name in JSON using Jackson
问题描述
我使用jackson将我的一个对象转换为json。
对象有2个字段:
@Entity
public class City {
@id
长ID;
字符串名称;
public String getName(){return name; }
public void setName(String name){this.name = name; }
public Long getId(){return id; }
public void setName(Long id){this.id = id; }
}
由于我想使用jQuery自动完成功能, id'在json和'name'中显示为'value',显示为'label'。杰克逊的文档不清楚,我已经尝试了每个注释,甚至远程似乎它做我所需要的,但我不能得到名称
显示为 label
和 id
在json中显示为 value
。 / p>
有人知道如何做到这一点吗?
p>你试过用@JsonProperty吗? @Entity
public class City {
@id
长ID;
字符串名称;
@JsonProperty(label)
public String getName(){return name; }
public void setName(String name){this.name = name; }
@JsonProperty(value)
public Long getId(){return id; }
public void setId(Long id){this.id = id; }
}
I'm using jackson to convert an object of mine to json. The object has 2 fields:
@Entity
public class City {
@id
Long id;
String name;
public String getName() { return name; }
public void setName(String name){ this.name = name; }
public Long getId() { return id; }
public void setName(Long id){ this.id = id; }
}
Since I want to use this with the jQuery auto complete feature I want 'id' to appear as 'value' in the json and 'name' to appear as 'label'. The documentation of jackson is not clear on this and I've tried every annotation that even remotely seems like it does what I need but I can't get name
to appear as label
and id
to appear as value
in the json.
Does anyone know how to do this or if this is possible?
Have you tried using @JsonProperty?
@Entity
public class City {
@id
Long id;
String name;
@JsonProperty("label")
public String getName() { return name; }
public void setName(String name){ this.name = name; }
@JsonProperty("value")
public Long getId() { return id; }
public void setId(Long id){ this.id = id; }
}
这篇关于使用Jackson更改JSON中的字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!