用resttemplate解析json [英] json parsing with resttemplate
本文介绍了用resttemplate解析json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下的json响应
I have a json response as below
{
"@odata.context": "some context value here",
"value": [{
"@odata.id": "odata id value1",
"@odata.etag": "W/\"CQEet/1EgOuA\"",
"Id": "id1",
"Subject": "subject1"
}, {
"@odata.id": "odata id value2",
"@odata.etag": "W/\"CyEet/1EgOEk1t/\"",
"Id": "id2",
"Subject": "subject2"
}]
}
如何使用Spring RestTemplate创建一个bean类(MyMessage)来解析值"?
How do I create a bean class(MyMessage) to parse the "value" using spring resttemplate?
RestTemplate rest = new RestTemplate();
ResponseEntity<MyMessage> response = rest.exchange(url, HttpMethod.GET, entity, MyMessage.class);
有人可以帮忙吗?
推荐答案
使用@JsonProperty
注释bean属性,以设置该属性的JSON字段名称(如果不同).
Annotate bean properties with @JsonProperty
to set JSON field name for the property if it is different.
请参阅:
JsonProperty注释和何时@使用的JsonProperty属性及其用途是什么?
示例(为简单起见,bean属性是公共的):
Example (bean properties are public for example simplicity):
MyMessage类:
MyMessage class:
public class MyMessage {
@JsonProperty("@odata.context")
public String context;
@JsonProperty("value")
public Value[] values;
}
值类别:
// PascalCaseStrategy is to resolve Id and Subject properties
@JsonNaming(PascalCaseStrategy.class)
public class Value {
@JsonProperty("@odata.id")
public String odataId;
@JsonProperty("@odata.etag")
public String odataEtag;
public String id;
public String subject;
}
这篇关于用resttemplate解析json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文