Jasper Reports JSON数据源获取空值 [英] Jasper Reports JSON Datasource get null values

查看:150
本文介绍了Jasper Reports 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>

现在你看到注释掉的行了

Now you see the commented out line


property name =net.sf.jasperreports.json.sourcevalue =emp.json

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



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

If i comment this in, everything works as intended, i dont 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查询执行器以java.io.InputStream的形式查找包含JSON源对象的JSON_INPUT_STREAM参数。如果未提供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"/>

您需要将文件作为 java.io.InputStream 参数 JSON_INPUT_STREAM

You need to pass the file as java.io.InputStream in the parameter 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);

这篇关于Jasper Reports JSON数据源获取空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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