Json array as comma separated values using JSONQL in JRXML

拟墨画扇 提交于 2020-01-16 13:58:28

问题


We have input json to jrxml as

{
  "ID": "5c2c7662f256a1452b0ed8b2",
  "MarketData": {
        "markets": [ { "id": "1001", "name": "val1" }, { "id": "1002", "name": "Val2" }, { "id": "1003", "name": "val3" } ],
        "products": [
        {"id": "1001", "proname": "shoe"},
        {"id": "1002", "name": "bag"},
        {"id": "1003", "name": "cap"}
        ],
        "location":"ABC"
    }
}

We have json that contains list of values of markets, products .

We need to show values from this json as Location : ABC Markets : val1,Val2,val3 Products Sold: shoes,bag,cap

Using this solution provided here i am able to display Markets as comma separated values Jasper converting list of values of json to comma separated values using jsonQL

Same technique if i use subdataset and display i get output as Location : ABC Markets : val1 val1,val2 val1,val2,val3 Products : shoes shoes,bag shoes,bag,cap

i declared the main data set as

<queryString language="jsonql">
        <![CDATA[]]>
    </queryString>
    <field name="location" class="java.lang.String">
        <property name="net.sf.jasperreports.jsonql.field.expression" value="location"/>
        <fieldDescription><![CDATA[location]]></fieldDescription>
    </field>

then subdataset as markets as

<subDataset name="marketList" uuid="01e76955-f29e-4d52-991b-aa0149bfdb37">
        <field name="name" class="java.lang.String">
            <property name="net.sf.jasperreports.jsonql.field.expression" value="name"/>
        </field>
        <variable name="allname" class="java.lang.String">
            <variableExpression><![CDATA[$V{allname} != null ? $V{allname} + ", " + $F{name} : $F{name}]]></variableExpression>
        </variable>
</subDataset>

referenced these fields in jrxml as

<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                            <datasetRun subDataset="marketList" uuid="565f0c3a-76b6-4527-b0d2-d306c5f4e20c">
                                <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonQLDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("$.MarketData.markets")]]></dataSourceExpression>
                            </datasetRun>
                            <jr:listContents height="16" width="103">
                                <textField>
                                    <reportElement x="0" y="0" width="100" height="16" uuid="7f2eaa2a-20b5-4598-bbf1-758f61dc3fc8"/>
                                    <textFieldExpression><![CDATA[$V{allname}]]></textFieldExpression>
                                </textField>
                            </jr:listContents>
                        </jr:list>

this gives output as

Markets : val1
          val1,val2
          val1,val2,val3

however expected output is

Location : ABC
Markets : val1,val2,val3
Products :shoes,bag,cap

How to show subdataset that has list of values as comma separated values .

Thanks Anjana


回答1:


In your list's textField you could set the evaluation time to something like Page or Report and display it only for the first record, something like so:

<jr:listContents height="0" width="180">
    <frame>
        <reportElement x="0" y="0" width="100" height="0" uuid="3c9872d0-ef6c-4643-9cf9-e78bc6996d3e">
            <property name="ShowOutOfBoundContent" value="true"/>
            <printWhenExpression><![CDATA[$V{REPORT_COUNT} == 1]]></printWhenExpression>
        </reportElement>
        <textField evaluationTime="Report">
            <reportElement x="0" y="0" width="100" height="16" uuid="f5a23577-4e57-4542-81a4-06ed679be920"/>
            <textFieldExpression><![CDATA[$V{allname}]]></textFieldExpression>
        </textField>
    </frame>
</jr:listContents>

The textField is wrapped in a 0 height frame to avoid unnecessary white space generation inside the list.



来源:https://stackoverflow.com/questions/54068628/json-array-as-comma-separated-values-using-jsonql-in-jrxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!