向Jaspersoft报表按索引添加JSON项

huangapple go评论55阅读模式
英文:

Adding JSON items to a Jaspersoft report by index

问题

我有一个JSON数组,像这样:

"platDist": [

{"name": "Wordpress", "total": 1360804},

{"name": "Bespoke", "total": 562864},

我试图实现的目标是在Jaspersoft报表中,将每个名称和总数显示在单独的文本字段中,而不是在表格或列表中显示整个内容。

我已经创建了一个新的报表,并将包含JSON数据的数据适配器附加到它。我可以将 'name' 字段拖放到 Detail 1 区域,它会显示为 $F{name},显示 'Wordpress' 和所有其他名称。

<textField>

<reportElement x="319" y="51" width="100" height="30" uuid="a76b2855-88bc-49ea-a70d-15d86d8f3c50">

<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="0c07b6de-eb6f-4c5b-a466-9020d88cf7f4"/>

</reportElement>

<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>

</textField>

在我的想法中,我希望它们都能按我想要的位置排列,我想象中应该很简单,比如:

<textFieldExpression><![CDATA[$F{name[0]}]]></textFieldExpression>

<textFieldExpression><![CDATA[$F{name[1]}]]></textFieldExpression>

<textFieldExpression><![CDATA[$F{name[2]}]]></textFieldExpression>

显然,这不起作用。我无法相信这是不可能的,但我更多是一个前端开发人员,对数据连接几乎没有经验,所以Jaspersoft的复杂性让我感到困惑。任何关于如何做到这一点的帮助或指导将不胜感激。 TIA.

英文:

I have a JSON array like this:

"platDist": [

{"name": "Wordpress", "total": 1360804},

{"name": "Bespoke", "total": 562864},

What I am trying to achieve is to have each name and total in separate text fields in a Jaspersoft report instead of displaying the whole thing in a table or list.

I have created a new report and attached a Data Adapter containing the JSON to it. I can drag the 'name' field into the Detail 1 band and it appears as $F{name}, showing 'Wordpress' and all the other names.

<textField>

<reportElement x="319" y="51" width="100" height="30" uuid="a76b2855-88bc-49ea-a70d-15d86d8f3c50">

<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="0c07b6de-eb6f-4c5b-a466-9020d88cf7f4"/>

</reportElement>

<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>

</textField>

In my head, what I would like to do is have them all positioned where I want them, and I imagined it would be something as simple as

<textFieldExpression><![CDATA[$F{name[0]}]]></textFieldExpression>

<textFieldExpression><![CDATA[$F{name[1]}]]></textFieldExpression>

<textFieldExpression><![CDATA[$F{name[2]}]]></textFieldExpression>

which, obviously, doesn't work. I can't believe this isn't possible, but I am more of a front end dev with very little experience of data connections, so a lot of the complexity of Jaspersoft is going over my head. Any help or guidance on how to do this will be very much appreciated.
TIA.

答案1

得分: 0

在最简单的情况下,可以通过以下方式实现:

  • 退出detail带(例如使用summary带)
  • 使用索引的field表达式,可以是相对的或绝对的(以$开头)

考虑以下JSON:

{
    "platDist": [
        {"name": "Wordpress", "total": 1360804},
        {"name": "Bespoke", "total": 562864},
        {"name": "Other", "total": 12345}
    ]
}

这是一个完整的示例:

<?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" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="3657c5d3-8f47-4baf-8261-21d46fd36db8">
    <queryString language="jsonql">
        <![CDATA[]]>
    </queryString>
    <field name="Name1" class="java.lang.String">
        <property name="net.sf.jasperreports.jsonql.field.expression" value="platDist[0].name"/>
    </field>
    <field name="Name2" class="java.lang.String">
        <property name="net.sf.jasperreports.jsonql.field.expression" value="platDist[1].name"/>
    </field>
    <field name="Name3" class="java.lang.String">
        <property name="net.sf.jasperreports.jsonql.field.expression" value="platDist[2].name"/>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="170" y="20" width="220" height="30" uuid="5240258f-73d0-4672-bda1-0e6cee344fe1"/>
                <textElement textAlignment="Center">
                    <font size="14"/>
                </textElement>
                <text><![CDATA[Indexed textField expression]]></text>
            </staticText>
        </band>
    </title>
    <summary>
        <band height="161" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="80" height="30" uuid="e1f1a6d0-83fb-4f57-aa9e-0404ac715a46"/>
                <text><![CDATA[Name1]]></text>
            </staticText>
            <textField>
                <reportElement mode="Opaque" x="80" y="0" width="100" height="30" backcolor="#DEFBFF" uuid="59ff204c-5118-40fd-89d1-92b6433d8e0e"/>
                <textFieldExpression><![CDATA[$F{Name1}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="0" y="40" width="80" height="30" uuid="e1f1a6d0-83fb-4f57-aa9e-0404ac715a46"/>
                <text><![CDATA[Name2]]></text>
            </staticText>
            <textField>
                <reportElement mode="Opaque" x="80" y="40" width="100" height="30" backcolor="#BDFBFC" uuid="e4fc53a7-458c-48af-874b-d1c9cda2fba3"/>
                <textFieldExpression><![CDATA[$F{Name2}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="0" y="80" width="80" height="30" uuid="e1f1a6d0-83fb-4f57-aa9e-0404ac715a46"/>
                <text><![CDATA[Name3]]></text>
            </staticText>
            <textField>
                <reportElement mode="Opaque" x="80" y="80" width="100" height="30" backcolor="#64E1FA" uuid="8368b190-d736-43be-a7ec-45fb0db865a8"/>
                <textFieldExpression><![CDATA[$F{Name3}]]></textFieldExpression>
            </textField>
        </band>
    </summary>
</jasperReport>

这将产生以下输出:

向Jaspersoft报表按索引添加JSON项

注意:因为这个示例不包含一个detail带,为了仍然生成输出,必须在报告级别设置whenNoDataType="AllSectionsNoDetail"

英文:

In the most simple case this is achievable by:

  • Stepping out of the detail band(use summary band for instance)
  • Using indexed field expressions either relative or absolute (that start with $)

Considering this JSON:

{
&quot;platDist&quot;: [
{&quot;name&quot;: &quot;Wordpress&quot;, &quot;total&quot;: 1360804},
{&quot;name&quot;: &quot;Bespoke&quot;, &quot;total&quot;: 562864},
{&quot;name&quot;: &quot;Other&quot;, &quot;total&quot;: 12345}
]
}

here's a complete example:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;jasperReport xmlns=&quot;http://jasperreports.sourceforge.net/jasperreports&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd&quot; name=&quot;Report&quot; pageWidth=&quot;595&quot; pageHeight=&quot;842&quot; whenNoDataType=&quot;AllSectionsNoDetail&quot; columnWidth=&quot;555&quot; leftMargin=&quot;20&quot; rightMargin=&quot;20&quot; topMargin=&quot;20&quot; bottomMargin=&quot;20&quot; uuid=&quot;3657c5d3-8f47-4baf-8261-21d46fd36db8&quot;&gt;
&lt;queryString language=&quot;jsonql&quot;&gt;
&lt;![CDATA[]]&gt;
&lt;/queryString&gt;
&lt;field name=&quot;Name1&quot; class=&quot;java.lang.String&quot;&gt;
&lt;property name=&quot;net.sf.jasperreports.jsonql.field.expression&quot; value=&quot;platDist[0].name&quot;/&gt;
&lt;/field&gt;
&lt;field name=&quot;Name2&quot; class=&quot;java.lang.String&quot;&gt;
&lt;property name=&quot;net.sf.jasperreports.jsonql.field.expression&quot; value=&quot;platDist[1].name&quot;/&gt;
&lt;/field&gt;
&lt;field name=&quot;Name3&quot; class=&quot;java.lang.String&quot;&gt;
&lt;property name=&quot;net.sf.jasperreports.jsonql.field.expression&quot; value=&quot;platDist[2].name&quot;/&gt;
&lt;/field&gt;
&lt;background&gt;
&lt;band splitType=&quot;Stretch&quot;/&gt;
&lt;/background&gt;
&lt;title&gt;
&lt;band height=&quot;79&quot; splitType=&quot;Stretch&quot;&gt;
&lt;staticText&gt;
&lt;reportElement x=&quot;170&quot; y=&quot;20&quot; width=&quot;220&quot; height=&quot;30&quot; uuid=&quot;5240258f-73d0-4672-bda1-0e6cee344fe1&quot;/&gt;
&lt;textElement textAlignment=&quot;Center&quot;&gt;
&lt;font size=&quot;14&quot;/&gt;
&lt;/textElement&gt;
&lt;text&gt;&lt;![CDATA[Indexed textField expression]]&gt;&lt;/text&gt;
&lt;/staticText&gt;
&lt;/band&gt;
&lt;/title&gt;
&lt;summary&gt;
&lt;band height=&quot;161&quot; splitType=&quot;Stretch&quot;&gt;
&lt;staticText&gt;
&lt;reportElement x=&quot;0&quot; y=&quot;0&quot; width=&quot;80&quot; height=&quot;30&quot; uuid=&quot;e1f1a6d0-83fb-4f57-aa9e-0404ac715a46&quot;/&gt;
&lt;text&gt;&lt;![CDATA[Name1]]&gt;&lt;/text&gt;
&lt;/staticText&gt;
&lt;textField&gt;
&lt;reportElement mode=&quot;Opaque&quot; x=&quot;80&quot; y=&quot;0&quot; width=&quot;100&quot; height=&quot;30&quot; backcolor=&quot;#DEFBFF&quot; uuid=&quot;59ff204c-5118-40fd-89d1-92b6433d8e0e&quot;/&gt;
&lt;textFieldExpression&gt;&lt;![CDATA[$F{Name1}]]&gt;&lt;/textFieldExpression&gt;
&lt;/textField&gt;
&lt;staticText&gt;
&lt;reportElement x=&quot;0&quot; y=&quot;40&quot; width=&quot;80&quot; height=&quot;30&quot; uuid=&quot;e1f1a6d0-83fb-4f57-aa9e-0404ac715a46&quot;/&gt;
&lt;text&gt;&lt;![CDATA[Name2]]&gt;&lt;/text&gt;
&lt;/staticText&gt;
&lt;textField&gt;
&lt;reportElement mode=&quot;Opaque&quot; x=&quot;80&quot; y=&quot;40&quot; width=&quot;100&quot; height=&quot;30&quot; backcolor=&quot;#BDFBFC&quot; uuid=&quot;e4fc53a7-458c-48af-874b-d1c9cda2fba3&quot;/&gt;
&lt;textFieldExpression&gt;&lt;![CDATA[$F{Name2}]]&gt;&lt;/textFieldExpression&gt;
&lt;/textField&gt;
&lt;staticText&gt;
&lt;reportElement x=&quot;0&quot; y=&quot;80&quot; width=&quot;80&quot; height=&quot;30&quot; uuid=&quot;e1f1a6d0-83fb-4f57-aa9e-0404ac715a46&quot;/&gt;
&lt;text&gt;&lt;![CDATA[Name3]]&gt;&lt;/text&gt;
&lt;/staticText&gt;
&lt;textField&gt;
&lt;reportElement mode=&quot;Opaque&quot; x=&quot;80&quot; y=&quot;80&quot; width=&quot;100&quot; height=&quot;30&quot; backcolor=&quot;#64E1FA&quot; uuid=&quot;8368b190-d736-43be-a7ec-45fb0db865a8&quot;/&gt;
&lt;textFieldExpression&gt;&lt;![CDATA[$F{Name3}]]&gt;&lt;/textFieldExpression&gt;
&lt;/textField&gt;
&lt;/band&gt;
&lt;/summary&gt;
&lt;/jasperReport&gt;

that produces this output:
向Jaspersoft报表按索引添加JSON项

A note: because this sample does not contain a detail band, in order for the output to still be produced, setting whenNoDataType=&quot;AllSectionsNoDetail&quot; at the report level is necessary.

huangapple
  • 本文由 发表于 2023年3月4日 02:11:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定