freemarker list转json

1. list转json处理

<#assign text>
{"msg":"OK","count":"2","totalpage":"1","userlist":[{"id":"1","name":"张三","age":"12"},{"id":"2","name":"李四","age":"20"}]}
</#assign>
<#assign json=text?eval />
<#list json.userlist as item>
id:${item.id}, name:${item.name},age:${item.age}
</#list>

简单的json格式数据在freemarker中可以直接解析,assign指令用于在页面上定义一个变量,这儿定义了一个名称为text的json格式变量,下面普通的json解析是属于后台传递过来。

2. json的list处理

@RequestMapping(method = RequestMethod.GET)
public ModelAndView index() {
	ModelAndView view = new ModelAndView("sc");
	Listlist = new ArrayList();
	TestVO contentVO = new TestVO();
	contentVO.setTitle("www.51gjie.com");
	list.add(contentVO);
	JSONObject json = new JSONObject();
	json.accumulate("list", list);
	view.addObject("message", json);
	return view;
}
//freemarker页面
<#list message.list as content>
   java教程网站:${content.title}
</#list>

输出:java教程网站:www.51gjie.com

版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。