I'm using jackson to convert an object of mine to json. The object has 2 fields:
我正在使用jackson将我的一个对象转换为json。对象有两个字段:
@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.
因为我想使用jQuery自动完成功能,所以我想要“id”在json中显示为“value”,而“name”作为“label”出现。jackson的文档不清楚这一点,我尝试过所有的注释,即使是远程的注释,似乎也能满足我的需要,但是我不能让名称作为标签显示,id作为json中的值显示。
Does anyone know how to do this or if this is possible?
有人知道怎么做吗?或者这是可能的?
3 个解决方案
#1
229
Have you tried using @JsonProperty?
你试过使用@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; }
}
#2
30
Be aware that there is org.codehaus.jackson.annotate.JsonProperty
in Jackson 1.x and com.fasterxml.jackson.annotation.JsonProperty
in Jackson 2.x. Check which ObjectMapper you are using (from which version), and make sure you use the proper annotation.
请注意,这里有org.codehaus.jackson.annotate。在杰克逊JsonProperty 1。x和com.fasterxml.jackson.annotation。JsonProperty杰克逊2. x。检查正在使用的ObjectMapper(来自哪个版本),并确保使用了正确的注释。
#3
5
There is one more option to rename field:
重命名字段还有一个选项:
杰克逊mixin。
Useful if you deal with third party classes, which you are not able to annotate, or you just do not want to pollute the class with Jackson specific annotations.
如果您处理第三方类(您无法对其进行注释),或者您只是不希望使用Jackson特定的注释污染类,那么这将非常有用。
#1
229
Have you tried using @JsonProperty?
你试过使用@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; }
}
#2
30
Be aware that there is org.codehaus.jackson.annotate.JsonProperty
in Jackson 1.x and com.fasterxml.jackson.annotation.JsonProperty
in Jackson 2.x. Check which ObjectMapper you are using (from which version), and make sure you use the proper annotation.
请注意,这里有org.codehaus.jackson.annotate。在杰克逊JsonProperty 1。x和com.fasterxml.jackson.annotation。JsonProperty杰克逊2. x。检查正在使用的ObjectMapper(来自哪个版本),并确保使用了正确的注释。
#3
5
There is one more option to rename field:
重命名字段还有一个选项:
杰克逊mixin。
Useful if you deal with third party classes, which you are not able to annotate, or you just do not want to pollute the class with Jackson specific annotations.
如果您处理第三方类(您无法对其进行注释),或者您只是不希望使用Jackson特定的注释污染类,那么这将非常有用。