在Jasper报告中的表中显示JSON数组

时间:2017-03-15 22:34:46

标签: java json jasper-reports

我正在尝试使用由JAVA函数传递的JSONDatasource创建一个JasperReport。

这是我发送给jasper的JSON

NotInfluenceReceiveTimeout

我正在尝试在表格中显示电话号码。我可以很好地显示其他字段。但不确定如何显示表格。

以下是我如何定义我的Jasper字段

{
  "Person": {
    "FirstName": "John",
    "LastName": "Smith",
    "Contacts": [
        {
            "Type": "Cell",
            "Number": "555-555-5555"
        },
        {
            "Type": "Home",
            "Number": "666-666-6666"
        }
    ]
  }
}

2 个答案:

答案 0 :(得分:3)

使用JSON语言,您只能通过subDataset访问Contacts嵌套数组,您可以使用它来填充Table组件。这是一个完整的示例(我已删除生成的样式以保持示例紧凑):

<?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="stackoverflow/JSON_array_table/DataAdapter.xml"/>
    <subDataset name="ContactsSubdataset" uuid="4563e834-a9e5-43b5-9f0a-824948c73c73">
        <field name="Type" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="Type"/>
        </field>
        <field name="Number" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="Number"/>
        </field>
    </subDataset>
    <queryString language="json">
        <![CDATA[]]>
    </queryString>
    <field name="FirstName" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="Person.FirstName"/>
    </field>
    <field name="LastName" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="Person.LastName"/>
    </field>
    <detail>
        <band height="300" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="80" height="30" uuid="d59811d6-a4e9-4871-a64b-f332d049e551"/>
                <text><![CDATA[First Name:]]></text>
            </staticText>
            <staticText>
                <reportElement x="0" y="30" width="80" height="30" uuid="ef25edff-1c33-4ec4-8dd1-21766039e6e3"/>
                <text><![CDATA[Last Name:]]></text>
            </staticText>
            <staticText>
                <reportElement x="0" y="60" width="80" height="30" uuid="e74bfa48-5040-4655-b419-f4f8e9f7e871"/>
                <text><![CDATA[Contacts:]]></text>
            </staticText>
            <textField>
                <reportElement x="80" y="0" width="200" height="30" uuid="49d1558c-396f-4864-b71f-41b6c7d677c2"/>
                <textFieldExpression><![CDATA[$F{FirstName}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="80" y="30" width="200" height="30" uuid="8ddaca45-a124-4c67-b2a8-50f7188097f9"/>
                <textFieldExpression><![CDATA[$F{LastName}]]></textFieldExpression>
            </textField>
            <componentElement>
                <reportElement x="80" y="60" width="200" height="60" uuid="105c3994-b8a8-4085-81ef-4a0e55efc7c6"/>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="ContactsSubdataset" uuid="5459bdfb-6835-4007-8e56-566716c8c29c">
                        <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("Person.Contacts")]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="100" uuid="b3d4c078-f65a-4213-9644-83b85b425703">
                        <jr:columnHeader height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="4b972b67-2f1a-4843-857e-c735dfc59c82"/>
                                <text><![CDATA[Type]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="e618a748-3273-476d-95c5-4d232260bf0a"/>
                                <textFieldExpression><![CDATA[$F{Type}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="100" uuid="5b01cf7c-71bc-474a-9858-ef58479efcdc">
                        <jr:columnHeader height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="394f2626-8c49-491d-a09c-ef7f90132741"/>
                                <text><![CDATA[Number]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="40ddb342-5be4-43bf-9fb2-a1fda76f53af"/>
                                <textFieldExpression><![CDATA[$F{Number}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

答案 1 :(得分:1)

这可能是因为联系人数据是在json-array

JSONObject jsonObject = new JSONObject(jsonString);
JSONArray contactsArray = jsonObject.getJSONArray("contacts");
JSONObject number = contactsArray.getJSONObject(1);
String numbers = number.getString("number");