使用freeMarker 实现json模板功能

freeMarker 中文手册

http://freemarker.foofun.cn/ref_directive_list.html

freeMarker有很多内建函数,可以满足大多数计算需求。下面使用一个例子进行展示

1.增加依赖包


    <dependencies>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.23</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>
    </dependencies>

2.代码实现

数据+模板=> 最终输出

import com.google.gson.Gson;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;

import java.io.StringWriter;
import java.util.Map;


public class Main {

    public static Configuration cfg;

    static {
        cfg = new Configuration(new Version("2.3.23"));
    }

    public static void main(String[] args) {
        String json = "{\n" +
                "    \"name\": \"BeJson\",\n" +
                "    \"url\": \"http://www.bejson.com\",\n" +
                "    \"page\": 88,\n" +
                "    \"isNonProfit\": true,\n" +
                "    \"address\": {\n" +
                "        \"street\": \"科技园路.\",\n" +
                "        \"city\": \"江苏苏州\",\n" +
                "        \"country\": \"中国\"\n" +
                "    },\n" +
                "    \"links\": [\n" +
                "        {\n" +
                "            \"name\": \"Google\",\n" +
                "            \"url\": \"http://www.google.com\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"Baidu\",\n" +
                "            \"url\": \"http://www.baidu.com\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"SoSo\",\n" +
                "            \"url\": \"http://www.SoSo.com\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        Map map = new Gson().fromJson(json, Map.class);
        String templae = "{\n" +
                "    \"name\": \"${name}\",\n" +
                "    \"url\": \"${url}\",\n" +
                "    \"page\": 88,\n" +
                "    \"isNonProfit\": true,\n" +
                "    \"address\": {\n" +
                "        \"street\": \"${address.street}\",\n" +
                "        \"city\": \"江苏苏州\",\n" +
                "        \"country\": \"中国\"\n" +
                "    },\n" +
                "    \"links\": [\n" +
                "       <#list links as  link >\n" +
                "      \t   <#if link.name  == \"Baidu\" && link.url = \"http://www.baidu.com\" >\n" +
                "         \t{\n" +
                "                \"name\": \"${link.name}\",\n" +
                "          \t     \"url\": \"${link.url}\"\n" +
                "            }<#sep>,\t\t\t \n" +
                "     \t\t</#if>\n" +
                "       </#list>\n" +
                "]\n" +
                "}";
        String result = processFreemarker(templae, map);
        System.out.println(result);
    }


    /**
     * Freemarker渲染模板
     *
     * @param template 模版
     * @param params   参数
     * @return
     */
    public static String processFreemarker(String template, Map<String, Object> params) {
        if (template == null || params == null)
            return null;
        try {
            StringWriter result = new StringWriter();
            Template tpl = new Template("strTpl", template, cfg);
            tpl.process(params, result);
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容