美文网首页
使用freeMarker 实现json模板功能

使用freeMarker 实现json模板功能

作者: 吃番茄的土拨鼠 | 来源:发表于2021-06-26 12:13 被阅读0次

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

    相关文章

      网友评论

          本文标题:使用freeMarker 实现json模板功能

          本文链接:https://www.haomeiwen.com/subject/gvqzyltx.html