【发布时间】:2019-11-04 19:27:56
【问题描述】:
我从天气 api 得到以下 Json 响应:
{ "data": [
{
"moonrise_ts": 1572876192,
"wind_cdir": "SE",
"rh": 84,
"pres": 982.153,
"high_temp": 9.3,
"sunset_ts": 1572884637,
"ozone": 322.467,
"moon_phase": 0.552016,
"wind_gust_spd": 6.28386,
"snow_depth": 0,
"clouds": 77,
"ts": 1572825660,
"sunrise_ts": 1572850839,
"app_min_temp": 8.1,
"wind_spd": 1.39257,
"pop": 40,
"wind_cdir_full": "southeast",
"slp": 985.03,
"valid_date": "2019-11-04",
"app_max_temp": 12.5,
"vis": 0,
"dewpt": 7.7,
"snow": 0,
"uv": 0.697746,
"weather": {
"icon": "c04d",
"code": 804,
"description": "Overcast clouds"
},
"wind_dir": 134,
"max_dhi": null,
"clouds_hi": 0,
"precip": 0.705078,
"low_temp": 8.1,
"max_temp": 12.5,
"moonset_ts": 1572907595,
"datetime": "2019-11-04",
"temp": 10.3,
"min_temp": 8.1,
"clouds_mid": 45,
"clouds_low": 54
},
{
"moonrise_ts": 1572964153,
"wind_cdir": "SW",
"rh": 86,
"pres": 993.705,
"high_temp": 12.4,
"sunset_ts": 1572970931,
"ozone": 323.897,
"moon_phase": 0.648931,
"wind_gust_spd": 10.783,
"snow_depth": 0,
"clouds": 91,
"ts": 1572912060,
"sunrise_ts": 1572937349,
"app_min_temp": 8,
"wind_spd": 2.9909,
"pop": 30,
"wind_cdir_full": "southwest",
"slp": 996.693,
"valid_date": "2019-11-05",
"app_max_temp": 12.4,
"vis": 0,
"dewpt": 7.5,
"snow": 0,
"uv": 1.5811,
"weather": {
"icon": "c04d",
"code": 804,
"description": "Overcast clouds"
},
"wind_dir": 226,
"max_dhi": null,
"clouds_hi": 4,
"precip": 0.427734,
"low_temp": 6,
"max_temp": 12.4,
"moonset_ts": 1572998028,
"datetime": "2019-11-05",
"temp": 9.6,
"min_temp": 8,
"clouds_mid": 41,
"clouds_low": 85
} ], "city_name": "London", "lon": "-0.09184", "timezone": "Europe/London", "lat": "51.51279", "country_code": "GB", "state_code": "ENG" }
预计 2 天。我将使用 16 天的,所以它会更长。但是我想拆分它,所以每天都是一个单独的对象,只有字段“city_name”、“valid_date”、“temp”、“clouds”和“pop”。怎么能做到这一点,特别是当数组嵌套并且“city_name”在它之外时。我有以下反序列化类,这显然不起作用:
public class WeatherDtoDeserializer extends StdDeserializer<WeatherDto> {
public WeatherDtoDeserializer() {
this(null);
}
public WeatherDtoDeserializer(Class<?> vc) {
super(vc);
}
@Override
public WeatherDto deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode weatherNode = jp.getCodec().readTree(jp);
WeatherDto weatherDto = new WeatherDto();
weatherDto.setCity(weatherNode.get("city_Name").textValue());
weatherDto.setDate(LocalDate.parse(weatherNode.get("data").get("valid_date").textValue()));
weatherDto.setTemperature(weatherNode.get("data").get("temp").intValue());
weatherDto.setCloudiness(weatherNode.get("data").get("clouds").intValue());
weatherDto.setRainfall(weatherNode.get("data").get("pop").intValue());
return weatherDto;
}
}
这也是我在 WeatherApiClient 中获取预测的方法:
public List<WeatherDto> getForecast(Airport airport) {
URI url = UriComponentsBuilder.fromHttpUrl(getBaseUrl() +
"city=" + airport.getCity() +
"&country=" + airport.getCountryCode() +
"&key=" + KEY)
.build()
.encode()
.toUri();
return Optional.ofNullable(restTemplate.getForObject(url, WeatherDto[].class))
.map(Arrays::asList)
.orElse(new ArrayList<>());
}
我在尝试获取预测并将其打印出来时遇到了错误: Error
提前谢谢你。
【问题讨论】:
-
你可以将城市保存在附加的类属性中,然后你可以在方法中访问它
-
@Serge 对不起,您所说的附加类属性是什么意思?和普通属性有什么区别?不好意思,我还在学java,很多东西都不懂。
-
或者另一种方法是将 json 放入 Map。像这样 `Map
map = mapper.readValue(json, Map.class);` 其中 json 是包含示例 json 的字符串。一旦你有了这张地图,你就可以做任何你想做的事情,将它转换成你想要的任何 POJO。这对你有用吗? -
你试过我的答案@Acek 吗?
-
@Deadpool 我现在正在努力解决它,但仍然出现错误:com.fasterxml.jackson.databind.exc.MismatchedInputException: 无法反序列化
com.kodilla.travel.dto.WeatherDto[]的实例超出 START_OBJECT 令牌