Spring Boot Java将响应转换为预期json

2skhul33  于 2023-02-08  发布在  Spring
关注(0)|答案(1)|浏览(155)

你好,我收到了来自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;
}
j0pj023g

j0pj023g1#

此实现仅将Google Places API响应中的一些属性Map到LocationAddressComponent对象。您可能需要根据需要添加更多Map逻辑。

if (addressComponentsArray.isArray()) {
    for (JsonNode jsonNode : addressComponentsArray) {
        ArrayNode types = (ArrayNode) jsonNode.get("types");

        if (types.contains("street_number")) {
            responseObject.setStreet_number(jsonNode.get("long_name").asText());
        } else if (types.contains("route")) {
            responseObject.setStreet_name(jsonNode.get("long_name").asText());
        } else if (types.contains("locality")) {
            responseObject.setSuburb(jsonNode.get("long_name").asText());
        } else if (types.contains("administrative_area_level_1")) {
            responseObject.setState(jsonNode.get("long_name").asText());
        } else if (types.contains("postal_code")) {
            responseObject.setPostcode(jsonNode.get("long_name").asText());
        }
    }
}

相关问题