我是靠谱客的博主 无奈羊,最近开发中收集的这篇文章主要介绍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中的字段名称所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部