如何将JSON字段映射到自定义对象属性? [英] How to map JSON fields to custom object properties?
本文介绍了如何将JSON字段映射到自定义对象属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有一些字段的简单 json
消息,并希望使用 spring-web $ c $将其映射到java对象c>。
I have a simple json
message with some fields, and want to map it to a java object using spring-web
.
问题:我的目标类字段的名称与int json响应的名称不同。我怎么能无论如何将它们映射到对象而不必重命名java中的字段?
Problem: my target classes fields are named differently than int he json response. How can I anyhow map them to the object without having to rename the fields in java?
是否有一些注释可以放在这里?
Is there some annotation that could be placed here?
{
"message":"ok"
}
public class JsonEntity {
//how to map the "message" json to this property?
private String value;
}
RestTemplate rest = new RestTemplate();
rest.getForObject(url, JsonEntity.class);
推荐答案
将JSON属性映射到带有不同的名称使用 @JsonProperty注释 ,您的代码将是:
To map a JSON property to a java object with a different name use @JsonProperty annotation, and your code will be :
public class JsonEntity {
@JsonProperty(value="message")
private String value;
}
这篇关于如何将JSON字段映射到自定义对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文