使用ireport在Json数据源中使用点的字段名称

时间:2015-06-08 07:23:30

标签: json jasper-reports datasource

我的示例json文件内容

{
"FlightLog":{
    "FlightLog.crewMembers":[

    ],
    "destinationStationCode":"JPN",
    "FlightLog.destinationStationCode":"IND",
    "FlightLog.originStationCode":"IND",
    "FlightLog._lockVersion":"0",
    "FlightLog.flightNum":"123"
}
}

我想获得FlightLog.FlightLog.originStationCode的价值。

例如: 当我声明如下字段名称时,我得到值" JPN"

<field name="FlightLog.destinationStationCode" class="java.lang.String">
    <fieldDescription><![CDATA[FlightLog.destinationStationCode]]></fieldDescription>
</field>

如果我声明如下字段,

<field name="destinationStationCode" class="java.lang.String">
    <fieldDescription><![CDATA[FlightLog.FlightLog.destinationStationCode]]></fieldDescription>
</field>

我对上述代码没有任何价值。

我尝试使用

<field name="destinationStationCode" class="java.lang.String">
    <fieldDescription><![CDATA[FlightLog[FlightLog.destinationStationCode]]]></fieldDescription>
</field>
<field name="destinationStationCode2" class="java.lang.String">
    <fieldDescription><![CDATA[FlightLog.FlightLog\\.destinationStationCode]]></fieldDescription>
</field>

我该怎么做?

UPDATE

我的jrxml是,

<?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="Test" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="062e3e2d-b648-49c7-b4a5-1d3c4fbdfc6b">
    <queryString language="json">
        <![CDATA[]]>
    </queryString>
    <field name="FlightLog" class="java.lang.Object">
        <fieldDescription><![CDATA[FlightLog]]></fieldDescription>
    </field>
    <detail>
        <band height="20" splitType="Stretch">
            <textField isStretchWithOverflow="true">
                <reportElement x="0" y="0" width="555" height="20" uuid="dc09b2c4-31b1-4458-8097-2e912e80b3a2"/>
                <textFieldExpression><![CDATA[$F{FlightLog}.get("FlightLog.destinationStationCode")]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

更新2

我无法将数据传递到子报告,例如:我有一个像下面这样的json,

"MaintLogResource":{
    "MaintLogResource._lockVersion":"0",
    "MaintLogResource.resource":[
                                 {
                                 "TEST" : "TEST1",
                                 "ResourceType.resourceTimeHours":3.0,
                                 "ResourceType.resourceDescription":"resourceDescription",
                                 "ResourceType.resourceCount":2,
                                 "ResourceType.resourceIdentifier":{
                                 "ResourceIdentifier.resourceIdentifier":"resourceIdentifier",
                                 "ResourceIdentifier.typeOfResourceIdentifier":"EQUIPMENT"
                                 }
                                 }
                                 ]
}

如何将其传递给子报告,

我试过以下,

((net.sf.jasperreports.engine.data.JsonDataSource)((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("MaintLogResource")).subDataSource("MaintLogResource.resource") 

但是上面的代码给了我错误,

net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression : 
    Source text : ((net.sf.jasperreports.engine.data.JsonDataSource)((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("MaintLogResource")).subDataSource("MaintLogResource")
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:203)
    at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:591)
    at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:559)
    at net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java:966)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:392)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:294)
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:259)
    at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:455)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2044)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:778)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:288)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:151)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:909)
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:126)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:464)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:300)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:757)
    at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:1003)
    at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)
    at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
Caused by: net.sf.jasperreports.engine.JRException: No node available. Iterate or rewind the data source.
    at net.sf.jasperreports.engine.data.JsonDataSource.subDataSource(JsonDataSource.java:445)
    at net.sf.jasperreports.engine.data.JsonDataSource$subDataSource$0.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at Logbook_1434518442194_713295.evaluate(calculator_Logbook_1434518442194_713295:311)
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:190)
    ... 19 more

如何将数据传递到子报告?

UPDATE3

我能够将数据传递到子报告,如下所示,

new net.sf.jasperreports.engine.data.JsonDataSource(new ByteArrayInputStream($F{MaintLogResource}.get("MaintLogResource.resource").toString().getBytes()), "")

当我用&#34;。&#34;我无法获得该值,而如果没有点的密钥我能够读取该值,例如:我能够获得密钥的值&#34; TEST&#34;但不适合其他人。

1 个答案:

答案 0 :(得分:2)

destinationStationCodeFlightLog中的密钥,因此可以直接访问。相同的规则应用于其他键,直到任何键具有句点符号,如FlightLog.destinationStationCode,因为句点具有其自己的含义(对象的访问属性)。因此,它没有给出任何价值(您可以通过任何其他字符替换句号来验证,说&#34; _&#34;,然后所有工作)。

要访问此媒体资源,建议您创建FlightLog类型的字段Object,例如

<field name="FlightLog" class="java.lang.Object">
    <fieldDescription><![CDATA[FlightLog]]></fieldDescription>
</field>

并访问其属性,如

<textField>
    <reportElement x="0" y="0" width="555" height="29" uuid="afa65308-c0ae-4375-b8e5-99af4b69e02e"/>
    <textFieldExpression><![CDATA[$F{FlightLog}.destinationStationCode]]></textFieldExpression>
</textField>
<textField>
    <reportElement x="0" y="29" width="555" height="29" uuid="2009cbd3-7fb7-4d9e-8067-b03937aefcaa"/>
    <textFieldExpression><![CDATA[$F{FlightLog}.get("FlightLog.destinationStationCode")]]></textFieldExpression>
</textField>

希望这有帮助。

我的档案 -

<强> JSON

{
"FlightLog":{
    "FlightLog.crewMembers":[

    ],
    "destinationStationCode":"JPN",
    "FlightLog.destinationStationCode":"IND",
    "FlightLog.originStationCode":"IND",
    "FlightLog._lockVersion":"0",
    "FlightLog.flightNum":"123"
}
}

<强> JRXML

<?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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="53cc1a16-1ec1-486c-82f6-51915fa4b069">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <field name="FlightLog" class="java.lang.Object">
        <fieldDescription><![CDATA[FlightLog]]></fieldDescription>
    </field>
    <detail>
        <band height="58" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="555" height="29" uuid="afa65308-c0ae-4375-b8e5-99af4b69e02e"/>
                <textFieldExpression><![CDATA[$F{FlightLog}.destinationStationCode]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="29" width="555" height="29" uuid="2009cbd3-7fb7-4d9e-8067-b03937aefcaa"/>
                <textFieldExpression><![CDATA[$F{FlightLog}.get("FlightLog.destinationStationCode")]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>