Jasper使用jsonQL将json的值列表转换为逗号分隔的值

问题描述:

我们将json作为值列表 "markets":[{"id":"1001","name":"val1"},{"id":"1002","name":"Val2"},{"id":"1003", "name":"val3"}]

we have json as list of values "markets": [ { "id": "1001", "name": "val1" }, { "id": "1002", "name": "Val2" }, { "id": "1003", "name": "val3" } ]

使用jasper报告列表组件,我们要显示名称属性逗号的这些值列表,并在一个文本字段中分隔.

using jasper report list component we want to display these list of values of name attribute comma separated in one text field.

对于现有列表,name的值仍会以我们希望的行形式出现 这样的单个文本字段 vale1,val2,val3

with existing list the values of name are still coming as rows we want it to be single text field like this vale1,val2,val3

我们如何使用jasper报告列表遍历值并在单个文本字段中显示为逗号分隔.

how can we use list of jasper report to loop through the values and show in single text fields as comma separated .

谢谢, Anjana

Thanks, Anjana

您不需要为此的列表,只需使用类似这样的变量即可:

You don't need a list for that, you could just use a variable like so:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
<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="3c4d8e3d-d3d5-4cb4-9b07-c67ce217d8dd">
    <queryString language="jsonql">
        <![CDATA[..name]]>
    </queryString>
    <field name="name" class="java.lang.String">
        <property name="net.sf.jasperreports.jsonql.field.expression" value="[0]"/>
    </field>
    <variable name="NameConcat" class="java.lang.String">
        <variableExpression><![CDATA[$V{NameConcat} != null ? $V{NameConcat} + ", " + $F{name} : $F{name}]]></variableExpression>
    </variable>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="180" y="10" width="200" height="30" uuid="fe01a39a-03e4-4a94-b9f7-3317caff6ae3"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14"/>
                </textElement>
                <text><![CDATA[Concatenated values test]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="30" splitType="Stretch">
            <staticText>
                <reportElement mode="Opaque" x="0" y="0" width="180" height="30" forecolor="#000000" backcolor="#9AFBFC" uuid="59c0da96-00aa-4959-bdc4-4c9b9f7a2e2c">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="748e4fce-3f0c-4df0-836a-45f9a92ac8b8"/>
                </reportElement>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <paragraph leftIndent="10"/>
                </textElement>
                <text><![CDATA[Concatenated names]]></text>
            </staticText>
        </band>
    </columnHeader>
    <summary>
        <band height="30">
            <textField isStretchWithOverflow="true">
                <reportElement isPrintRepeatedValues="false" x="0" y="0" width="180" height="30" uuid="2968b744-4b20-47cf-9fcb-05de58d606d0"/>
                <textElement verticalAlignment="Middle">
                    <paragraph leftIndent="10"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{NameConcat}]]></textFieldExpression>
            </textField>
        </band>
    </summary>
</jasperReport>

输出: