How to populate a report using a JSON data source without getting null values?

I use Jasper Reports to create a simple report in PDF format. 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 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);

However, my values ​​from the JSON file are not transferred to my pdf.

This is my template:

<?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.source" value = "emp.json"

If I comment on this, everything will work as intended, I don’t want to hardcode my JSON values ​​into a template, because later I want to get them from the recreation service, which is not ready yet. I don’t understand why the values ​​are not parsed in the report, instead I just get two null values.

+4
3

JasperReports - JSON ( 6.4.3)

JSON (. JsonQueryExecuter) - , JsonDataSource ( ). JsonQueryExecuterFactory. , JSON JSON_INPUT_STREAM, JSON java.io.InputStream. JSON_INPUT_STREAM , net.sf.jasperreports.json.source String , JSON. JsonQueryExecuter JsonDataSource .

:

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

java.io.InputStream JSON_INPUT_STREAM

, , -

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

JsonQLQueryExecuterFactory JsonQLQueryExecuterFactory

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

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

, 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"/>

0

Source: https://habr.com/ru/post/1612909/


All Articles