我是jasper报道的新手。
我正在尝试从子文件生成pdf,其中包含一些信息,如name
,class
等和数组marks
。我试图将标记数组渲染为表格。我的JSON是
{"data": {"name" : "Johny",
"class" : "A2",
"sub" : "cs",
"interest" : "films",
"marks" : [{
"subject" : "Maths",
"mark" : "24",
"grade" : "A",
"remarks" : "",
"slNo" : "1"
},
{
"subject" : "English",
"mark" : "24",
"grade" : "A",
"remarks" : "",
"slNo" : "2"
},
]
}}
我已将子数据源表达式设为
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource()]]></dataSourceExpression>
完整的jrxml - Here
现在我从java中调用它们,
File jsonFile = new File("/Volumes/Johny/Work/EclipseWorkspace/SecondReport/res/Marks.Json");
JasperDesign jasperDesign = JRXmlLoader.load(new File("/Volumes/Johny/Work/EclipseWorkspace/SecondReport/res/First.jrxml"));
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
parameters.put(JsonQueryExecuterFactory.JSON_INPUT_STREAM, new FileInputStream(jsonFile));
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters);
JasperExportManager.exportReportToPdfFile(jasperPrint, "/Users/johnykutty/Desktop/Sample2.pdf");
其他字段已填充,但表值变为空。
答案 0 :(得分:1)
从here找到答案
问题是我必须在dataSourceExpression
中传递数组的密钥。因此,dataSourceExpression
应该像
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("marks")]]></dataSourceExpression>
请注意subDataSource()
方法