【问题标题】:How to fill report using JSON datasource without getting null values?如何使用 JSON 数据源填充报告而不获取空值?
【发布时间】:2015-10-23 10:52:38
【问题描述】:

我正在使用 Jasper Reports 构建一个简单的 pdf 报告。我有一个如下所示的 JSON 文件:

{"employees": [
    {"firstName" : "John", "lastName" : "Doe"},
    {"firstName" : "Anna", "lastName" : "Smith"},
    {"firstName" : "Peter", "lastName" : "Jones"}
]}

我正在尝试这样阅读:

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);

但是我的 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>

现在你看到了注释掉的行

属性名称="net.sf.jasperreports.json.source" value="emp.json"

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

【问题讨论】:

    标签: java json jasper-reports


    【解决方案1】:

    来自JasperReports - JSON Data Source Sample (version 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"/>
    

    您需要在参数JSON_INPUT_STREAM中将文件作为java.io.InputStream传递

    因此您当前将其作为数据源传递,您应该尝试这样的方法

    params.put(JsonQueryExecuterFactory.JSON_INPUT_STREAM, new FileInputStream(file));
    JasperFillManager.fillReportToFile(jasperReport, params);
    

    如果您喜欢使用新的JsonQLQueryExecuterFactory JSONQL Data Source

    params.put(JsonQLQueryExecuterFactory.JSON_INPUT_STREAM, new FileInputStream(file));
    JasperFillManager.fillReportToFile(jasperReport, params);
    

    【讨论】:

      【解决方案2】:

      如果您将 json 字符串作为 InputStream 传递,那么它将起作用。

      String reportContents = "{}" //your json
      InputStream is = new ByteArrayInputStream(reportContent.getBytes());
      Map params = new HashMap();
      params.put(JsonQueryExecuterFactory.JSON_INPUT_STREAM, is);
      JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params);
      

      【讨论】:

        【解决方案3】:

        查看 here 以了解包装 JavaBean 对象集合的数据源实现。

        List<YourClass> yourBeanCollection = queryDataFromJSON();
        
        JRDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(yourBeanCollection);
        
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
                        reportParams, beanCollectionDataSource);
        

        并在报告模板中导入 java.util 并声明您在报告中“注入”的集合

        <import value="java.util.*"/>
        <field name="yourBeanCollection" class="java.util.List"/>
        

        也可以看看here 的例子

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-24
          • 1970-01-01
          相关资源
          最近更新 更多