如何将 JSON 数组值传递到 Jasper Reports 子报表中?

How to pass a JSON array value into a Jasper Reports subreport?

Jasper Reports 和子表有问题。

我正在使用以下 JSON 格式:

[
  {
    "dataObjects": [
      {
        "objectId": "025b3873ebe640aa9d6a34f9306548ae",
        "employeeFullName": "John Smith",
        "employeeGrade": "GS12"
      },
      {
        "objectId": "08b9300f62dc498f86c675ac5897b4c9",
        "employeeFullName": "Bill Johnson",
        "employeeGrade": "GS14"
      }
    ]
  },
  {
    "dataObjects": [
      ...
    ]
  },
  {
    "dataObjects": [
      ...
    ]
  },
  {
    "dataObjects": [
      ...
    ]
  }
]

数据 returns 作为四个 JSON 对象的数组。每个对象包含 .dataObjects

下查询的结果

在这种情况下,我需要能够将此数据显示为四个单独的子报表、图表。

我没有看到在数组的情况下将数据从父报表传递到子报表的方法。

重新格式化数据给我:

{
  "report0": {
    "dataObjects": [
      {
        "objectId": "025b3873ebe640aa9d6a34f9306548ae",
        "employeeFullName": "John Smith",
        "employeeGrade": "GS12"
      },
      {
        "objectId": "08b9300f62dc498f86c675ac5897b4c9",
        "employeeFullName": "Bill Johnson",
        "employeeGrade": "GS14"
      }
    ],
  },
  "report1": {
    "dataObjects": [
      ...
    ]
  },
  "report2": {
    "dataObjects": [
      ...
    ]
  },
  "report3": {
    "dataObjects": [
      ...
    ]
  }  
}

使用这个效果很好:

<subreport>
  <reportElement x="20" y="10" width="260" height="260" uuid="6a319da0-bd78-4f44-8445-e03b9a62cc0f"/>
  <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("report0.dataObjects")]]></dataSourceExpression>
  <subreportExpression><![CDATA["quad_1.jrxml"]]></subreportExpression>
</subreport>

但是我需要能够在不重新处理数据的情况下做到这一点,我需要这样的东西,但它不起作用:

<subreport>
  <reportElement x="20" y="10" width="260" height="260" uuid="6a319da0-bd78-4f44-8445-e03b9a62cc0f"/>
  <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("[0].dataObjects")]]></dataSourceExpression>
  <subreportExpression><![CDATA["quad_1.jrxml"]]></subreportExpression>
</subreport>            

广泛的谷歌搜索没有发现任何关于使用 json 列表的信息。想法?

这被标记为重复,但重复没有解决我遇到的问题。

我需要将数据传递给子报表,数组中的每个条目都转到一个单独的子报表。我可以按照描述解决这个问题,但要求是子报表。

这可以通过 JSONQL 来实现,QL 是初始 JSON 语言的更强大的替代品。

您的主报表可能如下所示(为了简洁起见,我重复使用相同的子报表):

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3  -->
<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="Report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="749076bd-1ea5-4847-85e1-cf88b11c9ef6">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="DataAdapter.xml"/>
    <queryString language="jsonql">
        <![CDATA[[0]]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <summary>
        <band height="160" splitType="Stretch">
            <subreport>
                <reportElement x="0" y="0" width="550" height="40" uuid="33bba077-fd38-4db3-b2ad-56c21a18105f"/>
                <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonQLDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("dataObjects")]]></dataSourceExpression>
                <subreportExpression><![CDATA["SubReport.jasper"]]></subreportExpression>
            </subreport>
            <subreport>
                <reportElement positionType="Float" x="0" y="40" width="550" height="40" uuid="bb8ab5c7-0fe8-4398-8efc-6d648e6a3a2c"/>
                <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonQLDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("^[1].dataObjects")]]></dataSourceExpression>
                <subreportExpression><![CDATA["SubReport.jasper"]]></subreportExpression>
            </subreport>
            <subreport>
                <reportElement positionType="Float" x="0" y="80" width="550" height="40" uuid="001d79d0-e850-41a9-8995-f55d6e96bc45"/>
                <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonQLDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("^[2].dataObjects")]]></dataSourceExpression>
                <subreportExpression><![CDATA["SubReport.jasper"]]></subreportExpression>
            </subreport>
            <subreport>
                <reportElement positionType="Float" x="0" y="120" width="550" height="40" uuid="4d7d085b-1694-40b2-9e10-81428b1a9165"/>
                <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonQLDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("^[3].dataObjects")]]></dataSourceExpression>
                <subreportExpression><![CDATA["SubReport.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </summary>
</jasperReport>

这里的重要部分是查询:

  • 主查询 [0] 非常明显,它只是 select 数组中的第一项,因此对于第一个子报表,您只需应用 subDatasource() dataObjects
  • 后续查询仅沿 JSON 树 (^) 向上移动到 select 要传递给其各自子报表的适当源对象