我正在使用Spring 2.0.5 Release作为Parent。我是Spring的新手。而且我正在使用此API https://www.metaweather.com/api/location/2487956/尝试使用Object Mapper进行显示,而RestTemplate出现了问题。
public void read() throws IOException {
RestTemplate rest = new RestTemplate();
ResponseEntity<String> response = rest.getForEntity("https://www.metaweather.com/api/location/2487956", String.class);
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(response.getBody());
JsonNode name = root.path("weather_state_name");
System.out.println(response.getBody());
}
答案 0 :(得分:0)
您的RestEntity很好,您没有正确解析您的响应。 weather_state_name位于名为solidated_weather的数组中。以下内容将满足您的需求。
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(response.getBody());
JsonNode consolidated_weather =root.get("consolidated_weather");
consolidated_weather.forEach(x -> {
System.out.println( x.get("weather_state_name"));
});