有如下代码: JSONArray ds = (JSONArray) json.get("DS"); 其中ds的值是[{"Station_Name":"耿马国家基本气象站","Station_Id_C":"56946","SUM_PRE_Time_2020":"166"}, {"Station_Name":"临沧国家基本气象站","Station_Id_C":"56951","SUM_PRE_Time_2020":"95.4"}, {"Station_Name":"沧源","Station_Id_C":"56944","SUM_PRE_Time_2020":"260.8"}, {"Station_Name":"双江","Station_Id_C":"56950","SUM_PRE_Time_2020":"70.4"}] 如何结合freemarker动态生成html.将里面对应的Station_Id_C,SUM_PRE_Time_2020的值展示成一张表格
时间: 2023-03-03 18:06:23 浏览: 66
可以使用Freemarker来遍历JSON数组,然后动态生成HTML表格,其中表格的每一行对应JSON数组中的一个元素,表格的列对应JSON对象的属性。
以下是一个示例代码片段,假设ds是JSON数组的变量名:
```html
<table>
<thead>
<tr>
<th>Station Id</th>
<th>SUM_PRE_Time_2020</th>
</tr>
</thead>
<tbody>
<#list ds as item>
<tr>
<td>${item.Station_Id_C}</td>
<td>${item.SUM_PRE_Time_2020}</td>
</tr>
</#list>
</tbody>
</table>
```
在上面的代码中,使用Freemarker的`<#list>`指令来遍历JSON数组。`${item.Station_Id_C}`和`${item.SUM_PRE_Time_2020}`是Freemarker的变量插值语法,用于引用JSON对象的属性值。
生成的HTML表格将包含一个表头和一个数据行,表头中有两个列:Station Id和SUM_PRE_Time_2020。数据行中的每一行对应JSON数组中的一个元素,包含两个列:一个是`Station_Id_C`属性的值,另一个是`SUM_PRE_Time_2020`属性的值。
相关问题
有如下代码: JSONArray ds = (JSONArray) json.get("DS"); 其中ds的值是[{"Station_Name":"耿马国家基本气象站","Station_Id_C":"56946","SUM_PRE_Time_2020":"166"}, {"Station_Name":"临沧国家基本气象站","Station_Id_C":"56951","SUM_PRE_Time_2020":"95.4"}, {"Station_Name":"沧源","Station_Id_C":"56944","SUM_PRE_Time_2020":"260.8"}, {"Station_Name":"双江","Station_Id_C":"56950","SUM_PRE_Time_2020":"70.4"}] 如何结合freemarker动态生成html.将里面对应的Station_Id_C,SUM_PRE_Time_2020的值展示成一张表格 其中springboot该怎么写
你可以使用Freemarker模板引擎和Spring Boot框架来动态生成HTML表格,具体实现步骤如下:
1. 首先,将JSON数组转换为Java List对象,可以使用阿里巴巴的fastjson库进行转换:
```java
JSONArray ds = (JSONArray) json.get("DS");
List<Map<String, String>> stationList = JSONObject.parseObject(ds.toJSONString(), new TypeReference<List<Map<String, String>>>(){});
```
2. 然后,在Spring Boot项目中创建一个Controller类,将List对象传递给Freemarker模板:
```java
@Controller
public class StationController {
@RequestMapping("/station")
public String showStationList(Model model) {
JSONArray ds = (JSONArray) json.get("DS");
List<Map<String, String>> stationList = JSONObject.parseObject(ds.toJSONString(), new TypeReference<List<Map<String, String>>>(){});
model.addAttribute("stationList", stationList);
return "station-list";
}
}
```
3. 创建Freemarker模板station-list.ftl,并在其中使用Freemarker的语法生成HTML表格:
```html
<table>
<thead>
<tr>
<th>站点名称</th>
<th>站点ID</th>
<th>降水量</th>
</tr>
</thead>
<tbody>
<#list stationList as station>
<tr>
<td>${station.Station_Name}</td>
<td>${station.Station_Id_C}</td>
<td>${station.SUM_PRE_Time_2020}</td>
</tr>
</#list>
</tbody>
</table>
```
4. 最后,在Spring Boot项目中配置Freemarker视图解析器:
```java
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public ViewResolver getViewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setPrefix("");
resolver.setSuffix(".ftl");
resolver.setCache(false);
resolver.setContentType("text/html; charset=utf-8");
return resolver;
}
@Bean
public FreeMarkerConfigurer getFreeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/views/");
configurer.setDefaultEncoding("UTF-8");
return configurer;
}
}
```
这样,当访问“/station”路径时,Spring Boot将会渲染station-list.ftl模板,动态生成HTML表格,并将结果返回给客户端。
for (JsonElement item : jsonArray) { obj = gson.fromJson(item, C); }
这是一个Java代码的for循环,其中jsonArray是一个JsonArray对象,包含了多个JSON对象,每个JSON对象可以被解析成C类的一个实例。循环中的JsonElement item表示jsonArray中的每个JSON对象,通过fromJson方法将其转换成C类的实例,并将结果赋值给obj变量,最终得到的obj是jsonArray中最后一个JSON对象解析出来的C类的实例。gson是一个Gson库中的类,提供了fromJson方法用于将JSON对象解析成Java对象。需要注意的是,C类必须提供对应的构造函数和变量,以便fromJson方法能够正确地解析JSON对象。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)