英文:
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>
这将产生以下输出:
注意:因为这个示例不包含一个detail带,为了仍然生成输出,必须在报告级别设置whenNoDataType="AllSectionsNoDetail"
。
英文:
In the most simple case this is achievable by:
- Stepping out of the
detail
band(usesummary
band for instance) - Using indexed
field
expressions either relative or absolute (that start with$
)
Considering this JSON:
{
"platDist": [
{"name": "Wordpress", "total": 1360804},
{"name": "Bespoke", "total": 562864},
{"name": "Other", "total": 12345}
]
}
here's a complete example:
<?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>
A note: because this sample does not contain a detail band, in order for the output to still be produced, setting whenNoDataType="AllSectionsNoDetail"
at the report level is necessary.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论