如何将ObjectNode代码写入json数据下面?

时间:2016-08-08 10:02:05

标签: json

我想生成如下的响应。如何使用ObjectNodeObjectMapper执行此操作?在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"
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

创建一个POJO来表示响应,使用相应的字段名称和类型,使用@JsonProperty注释字段,并使用writeValueAsString类的ObjectMapper方法将POJO序列化为一个JSON字符串。