如何遍历JSON数组以在jasperreports中创建重复报告 [英] How to iterate over JSON array to create repeating report in jasperreports

查看:72
本文介绍了如何遍历JSON数组以在jasperreports中创建重复报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的JSON数据

I have a JSON data with a following structure

[{
"a1":"b1",
"a2":"b2",
"details1":[{"a1":"b1"}],
"details2":[{"a2":"b2"}],
},
{
"a1":"b1",
"a2":"b2",
"details1":[{"a1":"b1"}],
"details2":[{"a2":"b2"}],
},
{
"a1":"b1",
"a2":"b2",
"details1":[{"a1":"b1"}],
"details2":[{"a2":"b2"}],
}]

我已经为details1和details2数组创建了单独的报告. 另外,我还为主表的一个元素创建了一个报告,该报告涉及作为子报告的details1和details2报告.

I have created separate reports for details1 and details2 arrays. Also I have create a report for one element of the main table refferring to details1 and details2 reports as a subreports.

问题在于主报告仅打印第一个元素中的a1和a2,然后打印所有元素中所有details1和details2表中的所有元素.

The problem is the main report is printing only the a1 and a2 from the first element and then prints all elements from all details1 and details2 tables from all elements.

我的目标是创建一个报表,该报表从主表的第一个元素开始打印a1,a2,details1,details2,然后从第二个元素依次打印,依此类推.我该如何实现?

My goal is to create a report that will print a1,a2, details1, details2 from first element of the main table, then the second one and so on. How can I achive that?

换句话说,如何在JSON数组上迭代相同的报告模板?

In other words, how can I iterate the same report template over the JSON array?

推荐答案

您不必创建子报表即可到达嵌套数组.一个更简单的解决方案是使用subDataset s.

You don't have to create subreports in order to reach the nested arrays. A simpler solution is to make use of subDatasets.

在您的情况下,您需要为JSON源中的每个详细信息键创建subDatasets:

In your case you need to create subDatasets for each details key in your JSON source:

<subDataset name="details1" uuid="4563e834-a9e5-43b5-9f0a-824948c73c73">
  <field name="A1" class="java.lang.String">
    <fieldDescription><![CDATA[a1]]></fieldDescription>
  </field>
</subDataset>
<subDataset name="details2" uuid="f703cb76-2a4a-44f1-9a42-227e180038d2">
  <field name="A2" class="java.lang.String">
    <fieldDescription><![CDATA[a2]]></fieldDescription>
  </field>
</subDataset>

您的主查询必须为空,以便遍历主JSON源中的每个对象:

Your main query has to be empty in order to iterate over each object in the main JSON source:

<queryString language="json">
  <![CDATA[]]>
</queryString>

然后,您需要使用一种知道如何处理表或列表之类的子数据集的结构.我在这里选择一个列表,因为它更易于使用.对于第一个子数据集,您将拥有:

You then need to use a structure that knows how to handle a subDataset like a table or a list. I'm choosing a list here since it easier to work with. For the first subDataset you would then have:

<componentElement>
  <reportElement x="90" y="40" width="333" height="20" uuid="c3237c70-6b2e-43e3-aa21-5092d8b91afc"/>
  <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
    <datasetRun subDataset="details1" uuid="f5fdc6a3-736f-43ce-b549-cd7332d19eb8">
      <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("details1")]]></dataSourceExpression>
    </datasetRun>
    <jr:listContents height="20" width="333">
      <textField>
        <reportElement x="10" y="0" width="130" height="20" uuid="07e3ff2a-3832-4b06-9275-cb1ee8e51cfe"/>
        <textFieldExpression><![CDATA[$F{A1}]]></textFieldExpression>
      </textField>
    </jr:listContents>
  </jr:list>
</componentElement>

对于第二个子数据集,列表组件是相同的.

For the second subDataset the list component is identical.

这是具有完整解决方案的简单JRXML:

Here is a simple JRXML with the complete solution:

<?xml version="1.0" encoding="UTF-8"?>
<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="81afe112-ee1b-4443-8d1c-cb6d9ab95dd8">
  <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JsonArrayDataAdapter.xml"/>
  <subDataset name="details1" uuid="4563e834-a9e5-43b5-9f0a-824948c73c73">
    <field name="A1" class="java.lang.String">
      <fieldDescription><![CDATA[a1]]></fieldDescription>
    </field>
  </subDataset>
  <subDataset name="details2" uuid="f703cb76-2a4a-44f1-9a42-227e180038d2">
    <field name="A2" class="java.lang.String">
      <fieldDescription><![CDATA[a2]]></fieldDescription>
    </field>
  </subDataset>
  <queryString language="json">
    <![CDATA[]]>
  </queryString>
  <field name="A1" class="java.lang.String">
    <fieldDescription><![CDATA[a1]]></fieldDescription>
  </field>
  <field name="A2" class="java.lang.String">
    <fieldDescription><![CDATA[a2]]></fieldDescription>
  </field>
  <detail>
    <band height="99" splitType="Stretch">
      <textField>
        <reportElement x="72" y="16" width="100" height="24" uuid="698866c8-7d26-4bc7-8727-b4a56d239a53"/>
        <textFieldExpression><![CDATA[$F{A1}]]></textFieldExpression>
      </textField>
      <textField>
        <reportElement x="190" y="16" width="100" height="24" uuid="e775c6c0-4058-4bc4-8c7a-d4d381fd6e66"/>
        <textFieldExpression><![CDATA[$F{A2}]]></textFieldExpression>
      </textField>
      <componentElement>
        <reportElement x="90" y="40" width="333" height="20" uuid="c3237c70-6b2e-43e3-aa21-5092d8b91afc"/>
        <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
          <datasetRun subDataset="details1" uuid="f5fdc6a3-736f-43ce-b549-cd7332d19eb8">
            <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("details1")]]></dataSourceExpression>
          </datasetRun>
          <jr:listContents height="20" width="333">
            <textField>
              <reportElement x="10" y="0" width="130" height="20" uuid="07e3ff2a-3832-4b06-9275-cb1ee8e51cfe"/>
              <textFieldExpression><![CDATA[$F{A1}]]></textFieldExpression>
            </textField>
          </jr:listContents>
        </jr:list>
      </componentElement>
      <componentElement>
        <reportElement x="90" y="60" width="333" height="20" uuid="38f3ac11-ad3e-464c-813a-46132f23783f"/>
        <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
          <datasetRun subDataset="details2" uuid="833a13c3-e9b8-4f56-9f8f-279d32d403e8">
            <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("details2")]]></dataSourceExpression>
          </datasetRun>
          <jr:listContents height="20" width="333">
            <textField>
              <reportElement x="10" y="0" width="130" height="20" uuid="3d9fb513-bfc9-4d95-a3da-16b95cf15e7c"/>
              <textFieldExpression><![CDATA[$F{A2}]]></textFieldExpression>
            </textField>
          </jr:listContents>
        </jr:list>
      </componentElement>
    </band>
  </detail>
</jasperReport>

这篇关于如何遍历JSON数组以在jasperreports中创建重复报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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