我使用的是恩恩JSON,我想更改由JSON生成的类上的字段的名称。
我只想改变一下
{"first_name": "Cristine", "last_name": "McVie"}因此,它映射到Java字段:
String firstName;
String lastName;我已经让所有东西都正常工作了(也就是说,如果我在JSON中使用camel-case,那么就可以正确地创建对象。)
我尝试过@JsonPropery和类上的@Named注释(基于注释中的建议),如下所示:
public class Person {
@Named("first_name")
private String firstName;
@Named("first_name")
public String getFirstName() {
return firstName;
}
@Named("first_name")
public void setFirstName(String firstName) {
this.firstName = firstName;
}这就是为什么我一开始没有看到@JsonProperty起作用的原因。这个应用程序运行在Eclipse调试模式下,我相信Eclipse会重新部署更新的代码,但是添加注释显然不足以触发更新。必须重新启动应用程序才能找到它。
发布于 2015-10-24 15:31:51
您需要在字段中添加SerializedName注释(如GSON)或JsonProperty注释(如杰克逊),如下所示:
import org.boon.json.annotations.JsonProperty;
import org.boon.json.annotations.SerializedName;
public static class Person {
@SerializedName("first_name")
String firstName;
@JsonProperty("last_name")
String lastName;
}您可以看到另一个示例在文件中。
https://stackoverflow.com/questions/33319698
复制相似问题