使用freeMarker 实现json模板功能

freeMarker 中文手册

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

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

1.增加依赖包


    
        
            org.freemarker
            freemarker
            2.3.23
        

        
            com.google.code.gson
            gson
            2.3.1
        
    

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\n" +
                "       \n" +
                "]\n" +
                "}";
        String result = processFreemarker(templae, map);
        System.out.println(result);
    }


    /**
     * Freemarker渲染模板
     *
     * @param template 模版
     * @param params   参数
     * @return
     */
    public static String processFreemarker(String template, Map 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;
        }
    }
}

你可能感兴趣的:(使用freeMarker 实现json模板功能)