gpt4 book ai didi

java - 过滤时很漂亮的 JSON 导入,但返回整批数据时则不然 + ID 中丑陋的括号

转载 作者:行者123 更新时间:2023-12-01 17:59:40 27 4
gpt4 key购买 nike

我正在努力实现以下两件事:

  1. 方法 getData() 返回格式良好的 JSON,如 getById() 和 getBetween 已经返回。
  2. 如何去掉 ID 周围那些丑陋的括号(我正在读取的原始 csv 文件附带它们),这样我就可以返回 Id 的 UUID,而不是像我目前所做的那样返回字符串。

代码如下:

import java.io.InputStreamReader;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.foobar.hotovo.Domain.PricePaid;
import com.foobar.hotovo.Exceptions.PriceNotFoundException;

@Service
public class MainService {
private static final String URL = "http://prod.publicdata.landregistry.gov.uk.s3-website-eu-west-1.amazonaws.com/pp-monthly-update-new-version.csv";

private List<PricePaid> getData() {
RestTemplate restTemplate = new RestTemplate();

return restTemplate.execute(URL, HttpMethod.GET, null, clientHttpResponse -> {
InputStreamReader reader = new InputStreamReader(clientHttpResponse.getBody());
CsvToBean<PricePaid> csvToBean = new CsvToBeanBuilder<PricePaid>(reader)
.withType(PricePaid.class)
.withSeparator(',')
.withIgnoreLeadingWhiteSpace(true)
.build();

return csvToBean.stream().collect(Collectors.toList());
});
}

private boolean isBetween(PricePaid pricePaid, LocalDate from, LocalDate to) {
LocalDate date = pricePaid.getDateOfTransfer();
return date.isAfter(from) && date.isBefore(to);
}

public List<PricePaid> getAll() {
return getData();
}

public PricePaid getById(String id) throws PriceNotFoundException {
return getData().stream()
.filter(pricePaid -> pricePaid.getId().equals(id))
.findFirst()
.orElseThrow(() -> new PriceNotFoundException("Item not found!"));
}

public List<PricePaid> getBetween(LocalDate from, LocalDate to) {
return getData().stream()
.filter(pricePaid -> isBetween(pricePaid, from, to))
.collect(Collectors.toList());
}
}

以下是 getData() 的输出示例:

[{"id":"{9DBAD221-7F8E-6EB3-E053-6B04A8C0F257}","price":96000,"dateOfTransfer":"2002-05-31","postCode":"SA62 3JW","propertyType":"F","oldNew":"Y","duration":"L","paon":"THE COACH HOUSE, 29","saon":"FLAT 5","street":"ENFIELD ROAD","locality":"BROAD HAVEN","city":"HAVERFORDWEST","district":"PEMBROKESHIRE","county":"PEMBROKESHIRE","ppd":"A","recordStatus":"A"},{"id":"{9DBAD221-8683-6EB3-E053-6B04A8C0F257}","price":59500,"dateOfTransfer":"2002-12-05","postCode":"EX31 2BS","propertyType":"F","oldNew":"N","duration":"L","paon":"16","saon":"FLAT 3","street":"STICKLEPATH HILL","locality":"STICKLEPATH","city":"BARNSTAPLE","district":"NORTH DEVON","county":"DEVON","ppd":"A","recordStatus":"A"},{"id":"{9DBAD221-B238-6EB3-E053-6B04A8C0F257}","price":240000,"dateOfTransfer":"2002-02-15","postCode":"GL7 5BL","propertyType":"S","oldNew":"N","duration":"F","paon":"76","saon":"","street":"CHURCH ROAD","locality":"QUENINGTON","city":"CIRENCESTER","district":"COTSWOLD","county":"GLOUCESTERSHIRE","ppd":"A","recordStatus":"A"},{"id":"{9DBAD221-CBFB-6EB3-E053-6B04A8C0F257}","price":292500,"dateOfTransfer":"2002-09-27","postCode":"WR11 8QH","propertyType":"T","oldNew":"N","duration":"F","paon":"BIG BARN","saon":"","street":"","locality":"ULLINGTON","city":"EVESHAM","district":"WYCHAVON","county":"WORCESTERSHIRE","ppd":"A","recordStatus":"A"},{"id":"{9DBAD221-E1D3-6EB3-E053-6B04A8C0F257}","price":145000,"dateOfTransfer":"2002-01-25","postCode":"PE12 8SN","propertyType":"D","oldNew":"N","duration":"F","paon":"SILVERCROFT","saon":"","street":"RAVENS DROVE","locality":"HOLBEACH FEN","city":"SPALDING","district":"SOUTH HOLLAND","county":"LINCOLNSHIRE","ppd":"A","recordStatus":"A"},{"id":"{9DBAD222-5429-6EB3-E053-6B04A8C0F257}","price":79950,"dateOfTransfer":"2002-07-17","postCode":"NE29 6XJ","propertyType":"S","oldNew":"N","duration":"F","paon":"54","saon":"","street":"BLUCHER ROAD","locality":"","city":"NORTH SHIELDS","district":"NORTH TYNESIDE","county":"TYNE AND WEAR","ppd":"A","recordStatus":"A"},{"id":"{9DBAD222-5451-6EB3-E053-6B04A8C0F257}","price":65000,"dateOfTransfer":"2002-11-13","postCode":"NE6 5XY","propertyType":"F","oldNew":"N","duration":"L","paon":"71","saon":"","street":"KING JOHN TERRACE","locality":"","city":"NEWCASTLE UPON TYNE","district":"NEWCASTLE UPON TYNE","county":"TYNE AND WEAR","ppd":"A","recordStatus":"A"},{"id":"{9DBAD222-5F1E-6EB3-E053-6B04A8C0F257}","price":17000,"dateOfTransfer":"2002-07-01","postCode":"SY23 5NJ","propertyType":"D","oldNew":"N","duration":"F","paon":"TYNEWYDD","saon":"","street":"","locality":"BETHANIA","city":"LLANON","district":"CEREDIGION","county":"CEREDIGION","ppd":"A","recordStatus":"A"},{"id":"{9DBAD222-7BB6-6EB3-E053-6B04A8C0F257}","price":33000,"dateOfTransfer":"2002-01-31","postCode":"HD3 4QJ","propertyType":"T","oldNew":"N","duration":"L","paon":"26 - 28","saon":"","street":"SCAR LANE","locality":"","city":"HUDDERSFIELD","district":"KIRKLEES","county":"WEST YORKSHIRE","ppd":"A","recordStatus":"A"},{"id":"{9DBAD221-5212-6EB3-E053-6B04A8C0F257}","price":127000,"dateOfTransfer":"2002-11-28","postCode":"BS6 5QZ","propertyType":"T","oldNew":"N","duration":"F","paon":"200","saon":"","street":"CHELTENHAM ROAD","locality":"","city":"BRISTOL","district":"CITY OF BRISTOL","county":"CITY OF BRISTOL","ppd":"A","recordStatus":"A"},{"id":"{9DBAD221-65C4-6EB3-E053-6B04A8C0F257}","price":105000,"dateOfTransfer":"2002-11-01","postCode":"PE19 7BH","propertyType":"D","oldNew":"Y","duration":"F","paon":"287A","saon":"","street":"GREAT NORTH ROAD","locality":"EATON FORD","city":"ST NEOTS","district":"HUNTINGDONSHIRE","county":"CAMBRIDGESHIRE","ppd":"A","recordStatus":"A"},{"id":"{9DBAD221-79A7-6EB3-E053-6B04A8C0F257}","price":365000,"dateOfTransfer":"2002-05-31","postCode":"LA23 2HB","propertyType":"D","oldNew":"N","duration":"F","paon":"30","saon":"","street":"CRAIG WALK","locality":"","city":"WINDERMERE","district":"SOUTH LAKELAND","county":"CUMBRIA","ppd":"A","recordStatus":"A"},{"id":"{9DBAD221-8270-6EB3-E053-6B04A8C0F257}","price":58000,"dateOfTransfer":"2002-05-13","postCode":"SA62 3JW","propertyType":"F","oldNew":"Y","duration":"L","paon":"THE COACH HOUSE, 29","saon":"FLAT 3","street":"ENFIELD ROAD","locality":"BROAD HAVEN","city":"HAVERFORDWEST","district":"PEMBROKESHIRE","county":"PEMBROKESHIRE","ppd":"A","recordStatus":"A"},{"id":"{9DBAD221-9E52-6EB3-E053-6B04A8C0F257}","price":173000,"dateOfTransfer":"2002-09-16","postCode":"BN27 4BP","propertyType":"D","oldNew":"N","duration":"F","paon":"SUNNYSIDE","saon":"","street":"HACKHURST LANE","locality":"LOWER DICKER","city":"HAILSHAM","district":"WEALDEN","county":"EAST SUSSEX","ppd":"A","recordStatus":"A"},{"id":"{9DBAD221-A6F1-6EB3-E053-6B04A8C0F257}","price":110200,"dateOfTransfer":"2002-02-28","postCode":"RM16 6LW","propertyType":"F","oldNew":"Y","duration":"L","paon":"5","saon":"","street":"SAN MARCOS DRIVE","locality":"CHAFFORD HUNDRED","city":"GRAYS","district":"THURROCK","county":"THURROCK","ppd":"A","recordStatus":"A"},{"id":"{9DBAD221-B31F-6EB3-E053-6B04A8C0F257}","price":475000,"dateOfTransfer":"2002-03-28","postCode":"GL50 4LB","propertyType":"D","oldNew":"Y","duration":"F","paon":"PARK WAY","saon":"","street":"WEST DRIVE","locality":"","city":"CHELTENHAM","district":"CHELTENHAM","county":"GLOUCESTERS

最佳答案

您可以使用 Gson 或 Jackson 等库来漂亮地打印 json 字符串:

Gson method

Jackson method

关于java - 过滤时很漂亮的 JSON 导入,但返回整批数据时则不然 + ID 中丑陋的括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60658891/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com