Jasper converting list of values of json to comma

2019-09-20 01:26发布

问题:

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

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

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

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

Thanks, Anjana

回答1:

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>

And the output: