你好,我收到了来自google places API的回复
"address_components": [
{
"long_name": "123",
"short_name": "123",
"types": ["street_number"]
},
{
"long_name": "Mulgoa Road",
"short_name": "Mulgoa Rd",
"types": ["route"]
},
{
"long_name": "Penrith",
"short_name": "Penrith",
"types": ["locality", "political"]
},
{
"long_name": "City of Penrith",
"short_name": "City of Penrith",
"types": ["administrative_area_level_2", "political"]
},
{
"long_name": "New South Wales",
"short_name": "NSW",
"types": ["administrative_area_level_1", "political"]
},
{
"long_name": "Australia",
"short_name": "AU",
"types": ["country", "political"]
},
{
"long_name": "2750",
"short_name": "2750",
"types": ["postal_code"]
}
],
预期React
{
"postcode": "4215",
"unit_number": null,
"floor_number": null,
"building_name": null,
"building_number": null,
"block_number": null,
"street_number": "123",
"street_name": "Nerang",
"street_type": "Street",
"state": "Queensland",
"suburb": "Southport",
"city": null,
"district": null,
"fullAddress": "123 Nerang St, Southport QLD 4215, Australia"
}
在types
数组的基础上,我想将值填充到响应格式中。这就是我现在尝试实现它的方式。
JsonNode resultData = new ObjectMapper().readTree(String.valueOf(response.getResponseBody()));
if (Objects.equals(resultData.get("status").asText(), "OK")) {
JsonNode addressComponentsArray = resultData.get("result").get("address_components");
LocationAddressComponent responseObject = new LocationAddressComponent();
if (addressComponentsArray.isArray()) {
for (JsonNode jsonNode : addressComponentsArray) {
Array res = jsonNode.get("types");
}
}
你能建议如何使用流或Jackson。
LocationComponentAddress.java
public class LocationAddressComponent {
String postcode;
String unit_number;
String floor_number;
String building_name;
String building_number;
String block_number;
String street_number;
String street_name;
String street_type;
String state;
String suburb;
String city;
String district;
String fullAddress;
}
1条答案
按热度按时间j0pj023g1#
此实现仅将Google Places API响应中的一些属性Map到LocationAddressComponent对象。您可能需要根据需要添加更多Map逻辑。