我想生成如下的响应。如何使用ObjectNode
和ObjectMapper
执行此操作?在java类中,我必须编写以下响应的编码:
{
"code": 200,
"time": "2016-05-27T11:15:36+10:00",
"data": {
"result": {
"addresses": {
"addressId": "180056310",
"addressSummaryType": "PrimaryAddressSummary",
"addressType": "Property Address with Number",
"status": "Confirmed",
"number": "6",
"numberTo": "8",
"streetName": "REGENT",
"streetType": "Street",
"locality": "WOLLONGONG",
"postcode": "2500",
"state": "NSW",
"addressType": "RM",
"subAddresses": {
`entercodehere`
}
},
"serviceLocation": {
"readForServiceDate": "2013-09-27",
"locationId": "LOC000136215004",
"customerServiceAreaId": "CSA200000010939",
"exchangeServiceArea": "WLGG",
"facsRealm": "74",
"latitude": "-34:25:25.7",
"longitude": "150:53:27.3"
}
}
}
}
答案 0 :(得分:0)
创建一个POJO来表示响应,使用相应的字段名称和类型,使用@JsonProperty
注释字段,并使用writeValueAsString
类的ObjectMapper
方法将POJO序列化为一个JSON字符串。