Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

java - Display JSON Array in Table on Jasper Report

I am trying to create a JasperReport using JSONDatasource that is passed by a JAVA function.

Here is my JSON that I am sending to jasper

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

I am trying to display the phone numbers in a table. I can display the the other fields fine. But not sure how to display the table.

Here is how I am defining my Jasper fields

<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>
    <field name="Contacts" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="Person.Contacts"/>
</field>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With the JSON language you can access the Contacts nested array only through the means of a subDataset, with which you can populate a Table component. Here is a complete sample(I have remove the generated styles to keep the sample compact):

<?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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...