如何在不获取空值的情况下使用JSON数据源填充报告? [英] How to fill report using JSON datasource without getting null values?

查看:84
本文介绍了如何在不获取空值的情况下使用JSON数据源填充报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Jasper Reports to build a simple report pdf. I have a JSON file that looks like this:

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

我正在尝试像这样阅读它:

And I'm trying to read it in like this:

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.

However my the values from the JSON file are not passed to my 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"值="emp.json"

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

如果我对此进行注释,那么一切都会按预期进行,我不想将JSON值硬编码到模板中,因为稍后我想从rest服务中获取它们,这还没有准备好.我不明白,为什么值没有被解析到报表中,相反,我只得到两个空值.

If I comment this in, everything works as intended, I don't want to hard code my JSON values into the template, because later on I want to get them from a rest service, that's not ready yet. I do not understand, why the values are not getting parsed into the report, instead i just get two null values.

推荐答案

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

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

The built-in JSON query executer (see the JsonQueryExecuter class) is a tool that uses the query string to produce a JsonDataSource instance, based on specific built-in parameters (or equivalent report properties). This query executer is registered via JsonQueryExecuterFactory factory class. In order to prepare the data source, the JSON query executer looks for the JSON_INPUT_STREAM parameter that contains the JSON source objects in the form of an java.io.InputStream. If no JSON_INPUT_STREAM parameter is provided, then the query executer looks for the alternate net.sf.jasperreports.json.source String parameter or report property that stores the path to the location of the JSON source file. JsonQueryExecuter runs the query over the input source and stores the result in an in-memory JsonDataSource object.

因此,如果您不想使用:

So if you do not want to use:

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

您需要在参数JSON_INPUT_STREAM

因此,您当前正在将其作为数据源传递,则应尝试类似的操作

Hence you are currently passing it as datasource you should try something like this

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

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

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

这篇关于如何在不获取空值的情况下使用JSON数据源填充报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆