Freemarker读取JSON数据
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
@Autowired
Configuration configuration;
//模板中不可出现‘’等特殊字符
public String generateContent(String templateName, Object param) {
if (StringUtils.isEmpty(templateName) || param == null) {
new RunException("模板名称不能为空");
}
try {
configuration.setClassForTemplateLoading(FreeMarkerConfigurer.class, basePackagePath);
Template template = configuration.getTemplate(templateName, "utf-8");
return FreeMarkerTemplateUtils.processTemplateIntoString(template, param);
} catch (Exception e) {
new RunException("读取模板错误");
}
return null;
}
{
"key": "37",
"year_x" : "2097",
"year_y" : "1295",
"month_x" : "2215",
"month_y" : "1295",
"day_x" : "2282",
"day_y" : "1295",
"formal_path" : "/dev/ioffice/file/YYZZ_KLSZ.png",
"final_path" : "/dev/ioffice/file/ZH_CWL_YYZZ_KLSZ.png"
}
String json = this.generateContent("模板路径+模板名称"+".ftl", null);
JSONObject JSONObj = JSONObject.parseObject(json);
String objValue = JSONObj.getString("key");