如何使用JSON数据源填充报表而不获取空值?

Blu*_*lue 4 java json jasper-reports

我正在使用Jasper Reports来构建一个简单的报告pdf.我有一个看起来像这样的JSON文件:

{"employees": [
    {"firstName" : "John", "lastName" : "Doe"},
    {"firstName" : "Anna", "lastName" : "Smith"},
    {"firstName" : "Peter", "lastName" : "Jones"}
]}
Run Code Online (Sandbox Code Playgroud)

而我正试着这样读:

File file = new File("E:/Workspaces/jasperPDFreport/src/main/resources/emp.json");
JsonDataSource datasource = new JsonDataSource(file);

JasperDesign jasperDesign = JRXmlLoader.load("E:/Workspaces/jasperPDFreport/src/main/resources/jsonTemplate.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
Map parameters = new HashMap();
JasperPrint jasperPrint;
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, datasource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "BasicReport.pdf");
JasperViewer.viewReport(jasperPrint);
Run Code Online (Sandbox Code Playgroud)

但是我的JSON文件中的值不会传递给我的pdf.

这是我的模板:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.1.1.final using JasperReports Library version 6.1.1  -->
<!-- 2015-10-22T13:45:32 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Blank_A4_2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9e494ebe-c1fb-4448-bcee-38994e9720f7">
    <!--property name="net.sf.jasperreports.json.source" value="emp.json"/-->
    <queryString language="json">
        <![CDATA[employees]]>
    </queryString>  
    <field name="firstName" class="java.lang.String">
        <fieldDescription><![CDATA[firstName]]></fieldDescription>
    </field>
    <field name="lastName" class="java.lang.String">
        <fieldDescription><![CDATA[lastName]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="125" splitType="Stretch">
            <textField>
                <reportElement x="100" y="0" width="100" height="30" uuid="02b279da-3795-4655-8571-5a36a3ef378c"/>
                <textFieldExpression><![CDATA[$F{firstName}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="0" y="0" width="100" height="30" uuid="671e61ad-8d8f-48cb-969f-78c05a516398"/>
                <text><![CDATA[firstName]]></text>
            </staticText>
            <textField>
                <reportElement x="100" y="30" width="100" height="30" uuid="9d53f46f-a252-48b3-9213-8c3092c29f49"/>
                <textFieldExpression><![CDATA[$F{lastName}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="0" y="30" width="100" height="30" uuid="3b49affb-685a-4df2-a872-c0e6fdcab94b"/>
                <text><![CDATA[lastName]]></text>
            </staticText>
        </band>
    </detail>
</jasperReport>
Run Code Online (Sandbox Code Playgroud)

现在你看到注释掉的行了

property name ="net.sf.jasperreports.json.source"value ="emp.json"

如果我对此进行评论,一切都按预期工作,我不想将我的JSON值硬编码到模板中,因为稍后我想从休息服务中获取它们,这还没有准备好.我不明白,为什么这些值没有被解析到报告中,而是我得到两个空值.

Pet*_*erg 8

来自JasperReports - JSON数据源示例(版本6.4.3)

内置的JSON查询执行器(请参阅JsonQueryExecuter类)是一个工具,它使用查询字符串根据特定的内置参数(或等效的报表属性)生成JsonDataSource实例.此查询执行程序通过JsonQueryExecuterFactory工厂类注册.为了准备数据源,JSON查询执行器以java.io.InputStream的形式查找包含JSON源对象的JSON_INPUT_STREAM参数.如果未提供JSON_INPUT_STREAM参数,则查询执行器将查找备用net.sf.jasperreports.json.source字符串参数或报告属性,该属性存储JSON源文件位置的路径.JsonQueryExecuter在输入源上运行查询,并将结果存储在内存中的JsonDataSource对象中.

所以,如果你不想使用:

<property name="net.sf.jasperreports.json.source" value="emp.json"/>
Run Code Online (Sandbox Code Playgroud)

您需要像java.io.InputStream参数一样传递文件JSON_INPUT_STREAM

因此,您当前正在将其作为数据源传递,您应该尝试这样的事情

params.put(JsonQueryExecuterFactory.JSON_INPUT_STREAM, new FileInputStream(file));
JasperFillManager.fillReportToFile(jasperReport, params);
Run Code Online (Sandbox Code Playgroud)

如果您喜欢使用新的JsonQLQueryExecuterFactory JSONQL数据源

params.put(JsonQLQueryExecuterFactory.JSON_INPUT_STREAM, new FileInputStream(file));
JasperFillManager.fillReportToFile(jasperReport, params);
Run Code Online (Sandbox Code Playgroud)