使用对象映射器解析复杂的JSON

时间:2018-09-11 19:39:31

标签: java json objectmapper

I am trying to parse complex JSON response using REST template.

`[
{
"message": "address_matched",
"location": "433 merwyn road merion station pa",
"resultcountformatted": "488",
"resultcount": 488,
"pagerdata": {
"Current": 1,
"Pages": 55,
"ResultStart": 0,
"ResultEnd": 0,
"TotalResults": 488,
"PageList": [
{
"PageNumber": "1",
"PageURL": "/realestatehomesforsale/19066-p001.html?query=geodist-aorder"
},
{
"PageNumber": "2",
"PageURL": "/realestatehomesforsale/19066-p002.html?query=geodist-aorder"
},
{
"PageNumber": "3",
"PageURL": "/realestatehomesforsale/19066-p003.html?query=geodist-aorder"
},
{
"PageNumber": "4",
"PageURL": "/realestatehomesforsale/19066-p004.html?query=geodist-aorder"
},
{
"PageNumber": "5",
"PageURL": "/realestatehomesforsale/19066-p005.html?query=geodist-aorder"
},
{
"PageNumber": "6",
"PageURL": "/realestatehomesforsale/19066-p006.html?query=geodist-aorder"
},
{
"PageNumber": "7",
"PageURL": "/realestatehomesforsale/19066-p007.html?query=geodist-aorder"
},
{
"PageNumber": "8",
"PageURL": "/realestatehomesforsale/19066-p008.html?query=geodist-aorder"
},
{
"PageNumber": "9",
"PageURL": "/realestatehomesforsale/19066-p009.html?query=geodist-aorder"
},
{
"PageNumber": "10",
"PageURL": "/realestatehomesforsale/19066-p010.html?query=geodist-aorder"
},
{
"PageNumber": "..."
},
{
"PageNumber": "20",
"PageURL": "/realestatehomesforsale/19066-p020.html?query=geodist-aorder"
},
{
"PageNumber": "30",
"PageURL": "/realestatehomesforsale/19066-p030.html?query=geodist-aorder"
},
{
"PageNumber": "40",
"PageURL": "/realestatehomesforsale/19066-p040.html?query=geodist-aorder"
},
{
"PageNumber": "50",
"PageURL": "/realestatehomesforsale/19066-p050.html?query=geodist-aorder"
},
{
"PageNumber": "55",
"PageURL": "/realestatehomesforsale/19066-p055.html?query=geodist-aorder"
}
],
"PagesPerSet": 10,
"ItemsPerPage": 9
},
"lat": 40.004202,
"office": {
"OfficeListingsCount": 0,
"OfficeListings": [],
"PageNumber": 1,
"PageSize": 6,
"OfficeName": "quantro.remax.com",
"VirtualOfficeAllowed": true,
"ShowListingInformationSponsoredby": true,
"OfficeDetailURL": "/realestateofficerealtor/quantroremaxcom-id68000000.html",
"Address1": "",
"City": "",
"State": "",
"Zip": "",
"Phone": "",
"LicenseNumber": "",
"OfficeID": 68000000,
"PhotoRef": "https://matrix.remax.net/profileimages/balloonImage.jpg",
"HasOfficeOwners": false,
"IsGeoCoded": false,
"OfficeGeoCheckLevelCode": 0,
"SiteURL": "quantro.remax.com",
"Protocol": "https",
"SiteID": "68000000",
"Lat": 0,
"Lng": 0,
"AgentCount": 0,
"HideSocialWall": false,
"ShowListings": false,
"OfficeOwners": [],
"OfficeManagers": [],
"SearchSiteURL": "www.remax.com",
"ShowDetailsURL": false,
"ShowLogo": true,
"ShowContact": true,
"AccountID": 0,
"TagLine": "",
"MowSiteID": 0,
"IsMow": false
},
"long": -75.254086
},
[
{
"IsHomeValue": true,
"IsForeclosure": false,
"SoldDateFormatted": "6/27/2002",
"PriceFormatted": "380,000",
"AVMPriceFormatted": "597,668",
"ListingDetailURL": "/realestatehomesforsale/433-merwyn-rd-merion-station-pa-19066-gid400024907235.html",
"GlobalListingDetailURL": "/realestatehomesforsale/433-merwyn-rd-merion-station-pa-19066-gid400024907235.html",
"sz_id": 400024907235,
"ListingID": 0,
"ValueListingTypeID": 0,
"Address": "433 MERWYN RD",
"City": "MERION STATION",
"street_name": "MERWYN RD",
"State": "PA",
"Zip": "19066",
"SqFt": "2897",
"SqFtFormatted": "2,897",
"avm": "597668",
"History": [],
"ValueSearchType": 1,
"ValueSearchTypeText": "NOT FOR SALE",
"SiteID": 0,
"YearBuilt": 1958,
"BedRooms": 5,
"BathRooms": 4,
"Latitude": 40.004202,
"Longitude": -75.254086
},
{
"IsHomeValue": true,
"IsForeclosure": false,
"SoldDateFormatted": "7/14/1999",
"PriceFormatted": "245,000",
"AVMPriceFormatted": "516,630",
"ListingDetailURL": "/realestatehomesforsale/429-merwyn-rd-merion-station-pa-19066-gid400024907236.html",
"GlobalListingDetailURL": "/realestatehomesforsale/429-merwyn-rd-merion-station-pa-19066-gid400024907236.html",
"sz_id": 400024907236,
"ListingID": 0,
"ValueListingTypeID": 0,
"Address": "429 MERWYN RD",
"City": "MERION STATION",
"street_name": "MERWYN RD",
"State": "PA",
"Zip": "19066",
"SqFt": "2313",
"SqFtFormatted": "2,313",
"avm": "516630",
"History": [],
"ValueSearchType": 1,
"ValueSearchTypeText": "NOT FOR SALE",
"SiteID": 0,
"YearBuilt": 1958,
"BedRooms": 4,
"BathRooms": 3,
"Latitude": 40.004024,
"Longitude": -75.253908
},
{
"IsHomeValue": true,
"IsForeclosure": false,
"PriceFormatted": "300,000",
"AVMPriceFormatted": "581,476",
"ListingDetailURL": "/realestatehomesforsale/516-narberth-ave-merion-station-pa-19066-gid400024908056.html",
"GlobalListingDetailURL": "/realestatehomesforsale/516-narberth-ave-merion-station-pa-19066-gid400024908056.html",
"sz_id": 400024908056,
"ListingID": 0,
"ValueListingTypeID": 0,
"Address": "516 NARBERTH AVE",
"City": "MERION STATION",
"street_name": "NARBERTH AVE",
"State": "PA",
"Zip": "19066",
"SqFt": "2996",
"SqFtFormatted": "2,996",
"avm": "581476",
"History": [],
"ValueSearchType": 1,
"ValueSearchTypeText": "NOT FOR SALE",
"SiteID": 0,
"YearBuilt": 1920,
"BedRooms": 5,
"BathRooms": 4,
"Latitude": 40.000613,
"Longitude": -75.258385
}
]
]`

对于上述尝试找出POJO的响应,我尝试了几个POJO层次结构来解析这些Json无法弄清楚的东西,有人可以建议我POJO来解析它吗

实际上JSON响应具有包含对象的JSON数组列表,并且再次包含没有对象的数组和很难使用对象映射器映射到该响应的任何映射的数组名称,我们能否获得帮助来为此响应构建对象模型。

1 个答案:

答案 0 :(得分:0)

您有三个选择:

  1. 请与API提供程序联系,以了解API的Swagger / OpenAPI定义。如果有,则可以使用Swagger Codegen生成代码。

  2. 查找API文档,并根据手工定义的模型编写Java类。

  3. 如果没有时间进行第2点,则可以使用quicktype,它直接从JSON示例生成代码。