我是靠谱客的博主 无奈羊,这篇文章主要介绍jackson改变json值,使用Jackson更改JSON中的字段名称,现在分享给大家,希望可以做个参考。

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值,使用Jackson更改JSON中的字段名称的全部内容,更多相关jackson改变json值,使用Jackson更改JSON中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部