I have a JSON attribute’s value in Java, I can get the value properly but I couldn’t send to JSP file..
My JSON:
periods =
{"number":1,"name":"Tonight","startTime":"2023-06-05T22:00:00-04:00","endTime":"2023-06-06T06:00:00-04:00","isDaytime":false,"temperature":63,"temperatureUnit":"F","temperatureTrend":"rising","probabilityOfPrecipitation":{"unitCode":"wmoUnit:percent","value":null},"dewpoint":{"unitCode":"wmoUnit:degC","value":8.333333333333334},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":50},"windSpeed":"9 mph","windDirection":"NW","icon":"https://api.weather.gov/icons/land/night/sct?size=medium","shortForecast":"Partly Cloudy","detailedForecast":"Partly cloudy. Low around 63, with temperatures rising to around 65 overnight. Northwest wind around 9 mph."}
{"number":2,"name":"Tuesday","startTime":"2023-06-06T06:00:00-04:00","endTime":"2023-06-06T18:00:00-04:00","isDaytime":true,"temperature":76,"temperatureUnit":"F","temperatureTrend":null,"probabilityOfPrecipitation":{"unitCode":"wmoUnit:percent","value":40},"dewpoint":{"unitCode":"wmoUnit:degC","value":10},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":52},"windSpeed":"7 to 15 mph","windDirection":"NW","icon":"https://api.weather.gov/icons/land/day/tsra_hi,40?size=medium","shortForecast":"Haze","detailedForecast":"Haze and a chance of rain showers and patchy smoke between 8am and 11am, then haze and patchy smoke and a chance of showers and thunderstorms. Mostly sunny, with a high near 76. Northwest wind 7 to 15 mph. Chance of precipitation is 40%."}
This is controller:
@RequestMapping("/show")
public ModelAndView showForecast(ModelMap model) throws IOException {
ModelAndView mv = new ModelAndView();
mv.setViewName("show");
ResponseEntity<String> responseEntity = restTemplate.getForEntity("https://api.golde.gov", String.class);
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> result = new HashMap<String, Object>();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNode properties = objectMapper.readTree(responseEntity.getBody()).get("properties");
JsonNode periods = properties.get("periods");
// Forecast[] forecasts = objectMapper.readValue(periods.toString(), Forecast[].class);
if (periods.isArray()) {
for (final JsonNode objNode : periods) {
System.out.println(objNode);
result = objectMapper.convertValue(objNode, new TypeReference<Map<String, Object>>(){});
}
// mv.addObject("result", result);
model.addAttribute("result", result);
}
return mv;
}
I want to send all values to jsp and i used:
model.addAttribute("results", result);
This is JSP file:
<c:forEach items="${results}" var="result">
<td>${result.number}</td> </br>
<td>${result.name}</td> </br>
<td>${result.startTime}</td> </br>
<td>${result.endTime}</td> </br>
<td>${result.isDaytime}</td> </br>
<td>${result.temperature}</td> </br>
<td>${result.temperatureUnit}</td> </br>
<td>${result.temperatureTrend}</td> </br>
<td>${result.windSpeed}</td> </br>
<td>${result.windDirection}</td> </br>
<img src="${result.icon}" alt="Image" height="50px" width="50px">
<td>${result.shortForecast}</td> </br>
<td>${result.detailedForecast}</td> </br>
</c:forEach>
However when i run i only received 1 row is:
{"number":3,"name":"Monday","startTime":"2023-06-12T06:00:00-04:00","endTime":"2023-06-12T18:00:00-04:00","isDaytime":true,"temperature":77,"temperatureUnit":"F","temperatureTrend":null,"probabilityOfPrecipitation":{"unitCode":"wmoUnit:percent"}
[![enter image description here][1]][1]
How I can get all JSON data
2
Answers
The reason is due to
<c:forEach items="${result}" var="result">
,you have made items and var using the same variable,that cause the problemChange
to
and in your jsp code block,using below code
I found the problem in your code, this block
you set the result variable value to the latest element of periods array. That’s why you only received one record in result map. To fix that, you can change the result variable to List<Map<String, Object>>. For example
and in the for loop