速度模板不工作

时间:2016-11-04 19:07:13

标签: java json velocity

我正在尝试使用velocity模板解析json。这是我的json字符串,

std::char_traits<>::compare

这是我的速度模板,

{\n  \"firstName\": \"Tom\",\n  \"lastName\": \"Geller\",\n  \"department\": \"Retail\",\n  \"manager\": \"Steve\",\n  \"joiningDate\": \"03/08/2011\",\n  \"employees\": [\n    {\n      \"firstName\": \"Paul\",\n      \"lastName\": \"Balmer\",\n      \"department\": \"Retail\",\n      \"manager\": \"Tom Geller\",\n      \"joiningDate\": \"06/21/2014\"\n    },\n    {\n      \"firstName\": \"Eric\",\n      \"lastName\": \"S\",\n      \"department\": \"Retail\",\n      \"manager\": \"Tom Geller\",\n      \"joiningDate\": \"09/13/2014\"\n    }\n  ]\n}

这是打印的输出。它没有打印报告员工,

$firstName $lastName belongs to $department Department. 
His manager is $manager and joining date is $joiningDate.

Employees reporting to him are, 
#foreach( $employee in $employees )
    $employee.firstName $employee.lastName
#end

这是java代码,

Tom Geller belongs to Retail Department. 
His manager is Steve and joining date is 03/08/2011.

Employees reporting to him are, 

2 个答案:

答案 0 :(得分:1)

由于$employees是JSONArray,我看到两个可能的原因:

  • 您使用的是Apache Velocity版本的1.7之前版本(在Velocity 1.7中添加了对Iterable接口的支持)
  • 您正在使用20150729之前的json.org版本(它是将Iterable接口添加到JSONArray类的版本)

答案 1 :(得分:0)

您可以使用json

将json数据转换为Map / List形式。