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

Field name with dot in Json datasource using ireport

我的样本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[=19=].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

如何将数据传递给子报表?

更新3

我可以像下面这样将数据传递给子报告,

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

当我读取带有“.”的键时我无法获得该值,而如果没有点的键我能够读取该值例如:我能够获得键 "TEST" 的值,但不能获得其他键的值。

destinationStationCodeFlightLog 中的一个键,因此可以直接访问。相同的规则适用于其他键,直到任何键都有句点符号,如 FlightLog.destinationStationCode,因为句点有其自己的含义(在对象上访问 属性)。因此它没有给出任何值(您可以通过用任何其他字符替换句点来验证,比如“_”,然后一切正常)。

要访问此 属性,我建议您创建一个类型为 Object 的字段 FlightLog,例如

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