【问题标题】:Jasper converting list of values of json to comma separated values using jsonQLJasper 使用 jsonQL 将 json 的值列表转换为逗号分隔的值
【发布时间】:2018-10-24 09:33:06
【问题描述】:

我们有 json 作为值列表 “市场”:[{“id”:“1001”,“名称”:“val1”},{“id”:“1002”,“名称”:“Val2”},{“id”:“1003”, “名称”:“val3”}]

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

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

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

谢谢, 安佳娜

【问题讨论】:

    标签: json list jasper-reports


    【解决方案1】:

    你不需要一个列表,你可以像这样使用一个变量:

    <?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>
    

    然后输出:

    【讨论】:

    • 谢谢纳西斯。然而,我考虑列表的原因是因为这个列表作为父结构的一部分,我们必须将名称和市场名称显示为文本字段。 “父”:{“名称”:“测试”,“市场”:[{“id”:“1001”,“名称”:“val1”},{“id”:“1002”,“名称”:“ Val2" }, { "id": "1003", "name": "val3" } } 我们可能需要一个子数据集来使用您的变量连接解决方​​案来引用市场。不确定如何在没有列表的文本字段中引用此子数据集
    • 您应该准确地发布您想要通过完整数据实现的目标,而不仅仅是片段。
    • 是的,我知道我会尝试您的意见。谢谢
    • 这解决了这个问题,但是如果有多个字段使用子数据集,则给出值列表向子数据集提出查询以获取多个值列表。stackoverflow.com/questions/54068628/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2021-10-10
    相关资源
    最近更新 更多