English 中文(简体)
How to get all elements in arrays type Map and send to JSP file by JSTL in Spring
原标题:

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%."}
 {"number":3,"name":"Tuesday Night","startTime":"2023-06-06T18:00:00-04:00","endTime":"2023-06-07T06:00:00-04:00","isDaytime":false,"temperature":60,"temperatureUnit":"F","temperatureTrend":null,"probabilityOfPrecipitation":{"unitCode":"wmoUnit:percent","value":40},"dewpoint":{"unitCode":"wmoUnit:degC","value":7.777777777777778},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":56},"windSpeed":"8 to 14 mph","windDirection":"NW","icon":"https://api.weather.gov/icons/land/night/tsra_hi,40/few?size=medium","shortForecast":"Haze then Mostly Clear","detailedForecast":"Haze and patchy smoke and a chance of showers and thunderstorms before 8pm. Mostly clear, with a low around 60. Northwest wind 8 to 14 mph. Chance of precipitation is 40%."}
 {"number":4,"name":"Wednesday","startTime":"2023-06-07T06:00:00-04:00","endTime":"2023-06-07T18:00:00-04:00","isDaytime":true,"temperature":76,"temperatureUnit":"F","temperatureTrend":null,"probabilityOfPrecipitation":{"unitCode":"wmoUnit:percent","value":20},"dewpoint":{"unitCode":"wmoUnit:degC","value":7.777777777777778},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":56},"windSpeed":"10 mph","windDirection":"NW","icon":"https://api.weather.gov/icons/land/day/sct/rain_showers,20?size=medium","shortForecast":"Mostly Sunny then Slight Chance Rain Showers","detailedForecast":"A slight chance of rain showers after 2pm. Mostly sunny, with a high near 76. Northwest wind around 10 mph. Chance of precipitation is 20%."}
 {"number":5,"name":"Wednesday Night","startTime":"2023-06-07T18:00:00-04:00","endTime":"2023-06-08T06:00:00-04:00","isDaytime":false,"temperature":56,"temperatureUnit":"F","temperatureTrend":"rising","probabilityOfPrecipitation":{"unitCode":"wmoUnit:percent","value":20},"dewpoint":{"unitCode":"wmoUnit:degC","value":8.333333333333334},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":67},"windSpeed":"6 to 10 mph","windDirection":"NW","icon":"https://api.weather.gov/icons/land/night/rain_showers,20/sct?size=medium","shortForecast":"Slight Chance Rain Showers then Partly Cloudy","detailedForecast":"A slight chance of rain showers before 8pm. Partly cloudy. Low around 56, with temperatures rising to around 58 overnight. Northwest wind 6 to 10 mph. Chance of precipitation is 20%."}
 {"number":6,"name":"Thursday","startTime":"2023-06-08T06:00:00-04:00","endTime":"2023-06-08T18:00:00-04:00","isDaytime":true,"temperature":72,"temperatureUnit":"F","temperatureTrend":"falling","probabilityOfPrecipitation":{"unitCode":"wmoUnit:percent","value":null},"dewpoint":{"unitCode":"wmoUnit:degC","value":9.444444444444445},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":69},"windSpeed":"6 mph","windDirection":"NW","icon":"https://api.weather.gov/icons/land/day/bkn/tsra_hi?size=medium","shortForecast":"Partly Sunny then Slight Chance Showers And Thunderstorms","detailedForecast":"A slight chance of rain showers between 2pm and 5pm, then a slight chance of showers and thunderstorms. Partly sunny. High near 72, with temperatures falling to around 69 in the afternoon."}
 {"number":7,"name":"Thursday Night","startTime":"2023-06-08T18:00:00-04:00","endTime":"2023-06-09T06:00:00-04:00","isDaytime":false,"temperature":56,"temperatureUnit":"F","temperatureTrend":null,"probabilityOfPrecipitation":{"unitCode":"wmoUnit:percent","value":null},"dewpoint":{"unitCode":"wmoUnit:degC","value":10.555555555555555},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":77},"windSpeed":"3 to 7 mph","windDirection":"W","icon":"https://api.weather.gov/icons/land/night/tsra_hi?size=medium","shortForecast":"Slight Chance Showers And Thunderstorms","detailedForecast":"A slight chance of showers and thunderstorms before 2am. Mostly cloudy, with a low around 56."}
 {"number":8,"name":"Friday","startTime":"2023-06-09T06:00:00-04:00","endTime":"2023-06-09T18:00:00-04:00","isDaytime":true,"temperature":72,"temperatureUnit":"F","temperatureTrend":null,"probabilityOfPrecipitation":{"unitCode":"wmoUnit:percent","value":40},"dewpoint":{"unitCode":"wmoUnit:degC","value":10.555555555555555},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":77},"windSpeed":"7 mph","windDirection":"N","icon":"https://api.weather.gov/icons/land/day/rain_showers,30/tsra_hi,40?size=medium","shortForecast":"Chance Rain Showers","detailedForecast":"A chance of rain showers between 8am and 2pm, then a chance of showers and thunderstorms. Partly sunny, with a high near 72. Chance of precipitation is 40%."}
 {"number":9,"name":"Friday Night","startTime":"2023-06-09T18:00:00-04:00","endTime":"2023-06-10T06:00:00-04:00","isDaytime":false,"temperature":57,"temperatureUnit":"F","temperatureTrend":null,"probabilityOfPrecipitation":{"unitCode":"wmoUnit:percent","value":40},"dewpoint":{"unitCode":"wmoUnit:degC","value":11.666666666666666},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":77},"windSpeed":"7 mph","windDirection":"NW","icon":"https://api.weather.gov/icons/land/night/tsra_hi,40/sct?size=medium","shortForecast":"Chance Showers And Thunderstorms then Partly Cloudy","detailedForecast":"A chance of showers and thunderstorms before 8pm. Partly cloudy, with a low around 57. Chance of precipitation is 40%."}
 {"number":10,"name":"Saturday","startTime":"2023-06-10T06:00:00-04:00","endTime":"2023-06-10T18:00:00-04:00","isDaytime":true,"temperature":77,"temperatureUnit":"F","temperatureTrend":null,"probabilityOfPrecipitation":{"unitCode":"wmoUnit:percent","value":null},"dewpoint":{"unitCode":"wmoUnit:degC","value":11.11111111111111},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":77},"windSpeed":"7 mph","windDirection":"W","icon":"https://api.weather.gov/icons/land/day/sct?size=medium","shortForecast":"Mostly Sunny","detailedForecast":"Mostly sunny, with a high near 77."}
 {"number":11,"name":"Saturday Night","startTime":"2023-06-10T18:00:00-04:00","endTime":"2023-06-11T06:00:00-04:00","isDaytime":false,"temperature":62,"temperatureUnit":"F","temperatureTrend":null,"probabilityOfPrecipitation":{"unitCode":"wmoUnit:percent","value":null},"dewpoint":{"unitCode":"wmoUnit:degC","value":12.222222222222221},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":68},"windSpeed":"6 mph","windDirection":"SW","icon":"https://api.weather.gov/icons/land/night/sct?size=medium","shortForecast":"Partly Cloudy","detailedForecast":"Partly cloudy, with a low around 62."}
 {"number":12,"name":"Sunday","startTime":"2023-06-11T06:00:00-04:00","endTime":"2023-06-11T18:00:00-04:00","isDaytime":true,"temperature":81,"temperatureUnit":"F","temperatureTrend":null,"probabilityOfPrecipitation":{"unitCode":"wmoUnit:percent","value":30},"dewpoint":{"unitCode":"wmoUnit:degC","value":13.88888888888889},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":68},"windSpeed":"6 to 13 mph","windDirection":"SW","icon":"https://api.weather.gov/icons/land/day/bkn/rain_showers,30?size=medium","shortForecast":"Partly Sunny then Chance Rain Showers","detailedForecast":"A chance of rain showers after 2pm. Partly sunny, with a high near 81. Chance of precipitation is 30%."}
 {"number":13,"name":"Sunday Night","startTime":"2023-06-11T18:00:00-04:00","endTime":"2023-06-12T06:00:00-04:00","isDaytime":false,"temperature":63,"temperatureUnit":"F","temperatureTrend":null,"probabilityOfPrecipitation":{"unitCode":"wmoUnit:percent","value":40},"dewpoint":{"unitCode":"wmoUnit:degC","value":14.444444444444445},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":70},"windSpeed":"12 mph","windDirection":"SW","icon":"https://api.weather.gov/icons/land/night/rain_showers,40?size=medium","shortForecast":"Chance Rain Showers","detailedForecast":"A chance of rain showers. Mostly cloudy, with a low around 63. Chance of precipitation is 40%."}
 {"number":14,"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","value":40},"dewpoint":{"unitCode":"wmoUnit:degC","value":13.333333333333334},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":75},"windSpeed":"12 mph","windDirection":"W","icon":"https://api.weather.gov/icons/land/day/rain_showers,40/rain_showers,30?size=medium","shortForecast":"Chance Rain Showers","detailedForecast":"A chance of rain showers. Mostly cloudy, with a high near 77. 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.weather.gov/gridpoints/OKX/33,35/forecast", 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("result", result);

This is JSP file:

<c:forEach items="${result}" 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":14,"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","value":40},"dewpoint":{"unitCode":"wmoUnit:degC","value":13.333333333333334},"relativeHumidity":{"unitCode":"wmoUnit:percent","value":75},"windSpeed":"12 mph","windDirection":"W","icon":"https://api.weather.gov/icons/land/day/rain_showers,40/rain_showers,30?size=medium","shortForecast":"Chance Rain Showers","detailedForecast":"A chance of rain showers. Mostly cloudy, with a high near 77. Chance of precipitation is 40%."}

How I can get all JSON data

问题回答

Change

model.addAttribute("result", result);

to

model.addAttribute("results", result);

and in your jsp code block,using below code

<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>




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签