当我使用json填充子报表时,我遇到了JsonDataSource问题。这个问题已经被问到,但这些解决方案对我没有用,所以我需要帮助。 我的json: { “JSON”:{ “人民”:[{ “名称”: “约瑟夫”},{ “名称”: “ILDA”}]}}
我的JAVA代码:
Map<String, Object> parameters = new HashMap<String, Object>();
JasperDesign design = JRXmlLoader.load("C:\\tmp\\master.jrxml");
JasperReport compiled = JasperCompileManager.compileReport(design);
List<Map<String, String>> mappaPersone = new ArrayList<Map<String, String>>();
Map<String, String> persona = new LinkedHashMap<String, String>();
persona.put("name", "Joseph");
Map<String, String> persona1 = new LinkedHashMap<String, String>();
persona1.put("name", "Ilda");
mappaPersone.add(persona);
mappaPersone.add(persona1);
Map<String, List<Map<String, String>>> mappaGen = new HashMap<String, List<Map<String, String>>>();
mappaGen.put("Peoples", mappaPersone);
Map<String, Map<String, List<Map<String, String>>>> jsonMap = new HashMap<String, Map<String, List<Map<String, String>>>>();
jsonMap.put("json", mappaGen);
Gson gson = new Gson();
String json = gson.toJson(jsonMap);
showMessage(json);
InputStream is = new ByteArrayInputStream(json.getBytes("UTF-8"));
JsonDataSource js = new JsonDataSource(is);
parameters.put("REP_DS", js);
JasperPrint report = JasperFillManager.fillReport(compiled, parameters,
new JREmptyDataSource());
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setConfiguration(configuration);
ExporterInput exporterInput = new SimpleExporterInput(report);
exporter.setExporterInput(exporterInput);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(
baos);
exporter.setExporterOutput(exporterOutput);
exporter.exportReport();
Exception in thread "main" net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: ((net.sf.jasperreports.engine.data.JsonDataSource)$P{REP_DS}).subDataSource("json.Peoples")
at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:264)
at net.sf.jasperreport`enter code here`s.engine.fill.JRCalculator.evaluate(JRCalculator.java:610)
at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:578)
at net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java:1015)
at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:454)
at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:345)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:258)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:454)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2029)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:755)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:262)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:122)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:551)
at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:411)
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:122)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:667)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:983)
at report.services.JSONTEST.generateReport(JSONTEST.java:105)
at report.services.JSONTEST.main(JSONTEST.java:33)
Caused by: net.sf.jasperreports.engine.JRException: No node available. Iterate or rewind the data source.
at net.sf.jasperreports.engine.data.JsonDataSource.subDataSource(JsonDataSource.java:455)
at master_1437312305950_565620.evaluate(master_1437312305950_565620:173)
at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:251)
... 18 more
我无法理解有什么问题...请帮助!
在我的主要报告jrxml中我有:
<parameter name="REP_DS" class="java.lang.Object"/>
和
<subreport>
<reportElement positionType="Float" x="0" y="0" width="555" height="100" uuid="3f4dece2-5563-4df9-9cda-15623e0f6127"/>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REP_DS}).subDataSource("json.Peoples")]]></dataSourceExpression>
<subreportExpression><![CDATA["C:\\tmp\\slave.jasper"]]></subreportExpression>
</subreport>
在子报告中:
<queryString language="json">
<![CDATA[Peoples]]>
</queryString>
<field name="name" class="java.lang.String"/>
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement x="35" y="33" width="100" height="20" uuid="32f70169-032d-4408-b84d-90ac06bbcad5"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</title>
不知道错误在哪里。
更新1: 我通过将JsonDataSource传递给fillReport方法解决了异常问题:
JasperPrint report = JasperFillManager.fillReport(compiled, parameters,
js);
现在进入.jrxml,我正在使用REPORT_DATA_SOURCE参数,所以我删除了REP_DS。
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("json.Peoples")]]></dataSourceExpression>
现在没有例外......但我有另一个问题。执行操作仅打印json列表的1个元素。它只打印约瑟夫而不是伊尔达。为什么?布局问题?如何制作可迭代的子报表? 不要失败......
答案 0 :(得分:0)
好的,现在我可以从json打印列表了。我不得不将字段放入细节带中,它的工作原理!