【问题标题】:Generate report from json taken from mysqlc从 mysqlc 获取的 json 生成报告
【发布时间】:2020-07-01 21:45:29
【问题描述】:

我的数据库中有一个表,其中包含带有 jsonobject 和 jsonarray 的字段,我需要将这些值转换为 jasperreport

<subDataset name="Dataset" uuid="06d196fb-4ae2-4735-9798-84a7072c4ea6">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="DataAdapter.xml"/>
    <parameter name="data" class="java.lang.String"/>
    <queryString>
        <![CDATA[SELECT data1,data2,data3,data4,data5,data6
        FROM ri_table WHERE  id = $P{data}]]>
    </queryString>
    <field name="data1" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="data1"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ri_pj_fugados"/>
    </field>
    <field name="data2" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="data2"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ri_pj_fugados"/>
    </field>
    <field name="data3" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="data3"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ri_pj_fugados"/>
    </field>
    <field name="data4" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="data4"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ri_pj_fugados"/>
    </field>
    <field name="data5" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.label" value="data5"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="ri_pj_fugados"/>
    </field>
</subDataset>
<parameter name="data" class="java.lang.String"/>
<queryString>
    <![CDATA[SELECT data1,data2,data3,data4,data5,data6
        FROM ri_table WHERE  id = $P{data}]]>
</queryString>

这样我从数据库中提取数据

<summary>
    <band height="50">
        <property name="com.jaspersoft.studio.layout"/>
        <componentElement>
            <reportElement x="0" y="0" width="572" height="50" forecolor="#595959" uuid="1326f896-5433-4071-b733-25f1c836a830">
                <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                <property name="com.jaspersoft.studio.table.style.table_header" value="Table 1_TH"/>
                <property name="com.jaspersoft.studio.table.style.column_header" value="Table 1_CH"/>
                <property name="com.jaspersoft.studio.table.style.detail" value="Table 1_TD"/>
                <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
            </reportElement>
            <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="Dataset" uuid="e3de465d-85b0-414f-9f7e-704f827b366a">
                    <datasetParameter name="data">
                        <datasetParameterExpression><![CDATA[$P{data}]]></datasetParameterExpression>
                    </datasetParameter>
                    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                </datasetRun>
                <jr:column width="70" uuid="300d2e97-ff2d-438b-8528-d565f835ccc8">
                    <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
                    <jr:columnHeader style="Table 1_CH" height="20" rowSpan="1">
                        <staticText>
                            <reportElement x="0" y="0" width="70" height="20" forecolor="#FFFFFF" uuid="5c21d9d0-510d-4852-904e-21a49992c04e"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font size="8" isBold="true"/>
                            </textElement>
                            <text><![CDATA[data1]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="Table 1_TD" height="30">
                        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                            <reportElement mode="Transparent" x="0" y="0" width="70" height="30" forecolor="#595959" backcolor="#FAF5F5" uuid="4b036592-a8fa-405c-ac85-12f7cda5dabd"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font size="6"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{data1}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="90" uuid="300d2e97-ff2d-438b-8528-d565f835ccc8">
                    <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
                    <jr:columnHeader style="Table 1_CH" height="20" rowSpan="1">
                        <staticText>
                            <reportElement x="0" y="0" width="90" height="20" forecolor="#FFFFFF" uuid="5c21d9d0-510d-4852-904e-21a49992c04e"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font size="8" isBold="true"/>
                            </textElement>
                            <text><![CDATA[data2]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="Table 1_TD" height="30">
                        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                            <reportElement mode="Transparent" x="0" y="0" width="90" height="30" forecolor="#595959" backcolor="#FAF5F5" uuid="4b036592-a8fa-405c-ac85-12f7cda5dabd"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font size="6"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{data2}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="70" uuid="300d2e97-ff2d-438b-8528-d565f835ccc8">
                    <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
                    <jr:columnHeader style="Table 1_CH" height="20" rowSpan="1">
                        <staticText>
                            <reportElement x="0" y="0" width="70" height="20" forecolor="#FFFFFF" uuid="5c21d9d0-510d-4852-904e-21a49992c04e"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font size="8" isBold="true"/>
                            </textElement>
                            <text><![CDATA[data3]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="Table 1_TD" height="30">
                        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                            <reportElement mode="Transparent" x="0" y="0" width="70" height="30" forecolor="#595959" backcolor="#FAF5F5" uuid="4b036592-a8fa-405c-ac85-12f7cda5dabd"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font size="6"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{data3}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="41" uuid="300d2e97-ff2d-438b-8528-d565f835ccc8">
                    <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
                    <jr:columnHeader style="Table 1_CH" height="20" rowSpan="1">
                        <staticText>
                            <reportElement x="0" y="0" width="41" height="20" forecolor="#FFFFFF" uuid="5c21d9d0-510d-4852-904e-21a49992c04e"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font size="8" isBold="true"/>
                            </textElement>
                            <text><![CDATA[data4]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="Table 1_TD" height="30">
                        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                            <reportElement mode="Transparent" x="0" y="0" width="41" height="30" forecolor="#595959" backcolor="#FAF5F5" uuid="4b036592-a8fa-405c-ac85-12f7cda5dabd"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font size="6"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{data4}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="61" uuid="300d2e97-ff2d-438b-8528-d565f835ccc8">
                    <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
                    <jr:columnHeader style="Table 1_CH" height="20" rowSpan="1">
                        <staticText>
                            <reportElement x="0" y="0" width="61" height="20" forecolor="#FFFFFF" uuid="5c21d9d0-510d-4852-904e-21a49992c04e"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font size="8" isBold="true"/>
                            </textElement>
                            <text><![CDATA[data5]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="Table 1_TD" height="30">
                        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                            <reportElement mode="Transparent" x="0" y="0" width="61" height="30" forecolor="#595959" backcolor="#FAF5F5" uuid="4b036592-a8fa-405c-ac85-12f7cda5dabd"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font size="6"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{data5}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
            </jr:table>
        </componentElement>
    </band>
</summary>

这样它会创建一个包含生成字段的表,但在 json 数据中它会以 json 格式打印它们 我需要在某些情况下打印 data2.dato 或 data2.otherdato

我不知道怎么做,json数组也是一样, 我需要迭代数组中的 jsons 并打印出某些数据。

【问题讨论】:

    标签: mysql arrays json jasper-reports


    【解决方案1】:

    对于您的主查询中的那些字段,您知道它们是 JSON 格式,您可以创建一个 JSON/JSONQL 数据源并将其传递给子报表/表/列表/等。

    主要步骤是:

    1. 为该特定字段或字段类别创建subDataset,例如:

       <subDataset name="JsonFieldDataset" uuid="70d9d3c4-af50-48fc-a8a9-205697b62f0c">
           <field name="dato" class="java.lang.String">
               <property name="net.sf.jasperreports.jsonql.field.expression" value="dato"/>
           </field>
           <field name="otherDato" class="java.lang.String">
               <property name="net.sf.jasperreports.jsonql.field.expression" value="otherDato"/>
           </field>
       </subDataset>
      
    2. 在详细信息带中有一个构造 JSON/JSONQL 数据源的组件。仅发布带有相关部分的简化输出:

       <componentElement>
           <reportElement />
           <jr:table>
               <datasetRun subDataset="JsonFieldDataset" uuid="318415a6-a046-4886-b04a-f13c3e9bf6f0">
                   <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JsonQLDataSource(new ByteArrayInputStream($F{data2}.getBytes("UTF-8")))]]></dataSourceExpression>
               </datasetRun>
               <jr:column />
               <jr:column />                
           </jr:table>
       </componentElement>
      

    【讨论】:

      猜你喜欢
      • 2012-06-09
      • 2022-11-22
      • 2013-02-15
      • 1970-01-01
      • 2023-02-09
      • 2020-09-29
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多