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

时间:2021-01-15 01:01:57

My sample json file content

我的示例json文件内容

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

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

I want to get value for FlightLog.FlightLog.originStationCode.

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

for eg: When i declare field name as below , i am getting the value "JPN"

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

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

Whereas if i declare field as below,

如果我声明如下字段,

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

I am not getting any value for the above code.

我没有得到上述代码的任何价值。

I tried using

我试过用

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

How can i do this?

我怎样才能做到这一点?

UPDATE

My jrxml is,

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

UPDATE 2

I am not able to pass data to subreport, eg: I have a json like below,

我无法将数据传递到子报告,例如:我有一个像下面的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"
                                 }
                                 }
                                 ]
}

How do i pass it to sub report,

我如何将其传递给子报告,

i tried like below,

我尝试过如下,

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

But the above code is giving me error,

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

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

How can i pass data to subreport?

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

UPDATE3

I am able to pass data to sub report like below,

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

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

when i read a key with "." i cannot get the value, whereas if the key without dot i'm able to read the value eg: i'm able to get value for the key "TEST" but not for others.

当我用“。”读一把钥匙。我无法获得该值,而如果没有点的键我能够读取值,例如:我能够获得键“TEST”的值,但不能获得其他值。

1 个解决方案

#1


2  

destinationStationCode is a key in FlightLog and hence it is directly accessible. Same rule is applied on other keys, until any key have period sign, like FlightLog.destinationStationCode, because period have its own meaning (access property on the object). And therefore it is not giving any value (You can verify by replacing period by any other characket, say "_", then all works).

destinationStationCode是FlightLog中的一个键,因此可以直接访问。同样的规则应用于其他键,直到任何键都有周期符号,如FlightLog.destinationStationCode,因为句点有其自己的含义(对象的访问属性)。因此它没有给出任何价值(您可以通过任何其他字符替换句号来验证,说“_”,然后所有工作)。

To access this property, I suggest you to create an field FlightLog of type Object, like

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

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

and access its properties like

并访问其属性,如

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

Hope this helps.

希望这可以帮助。

My files --

我的文件 -

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>

#1


2  

destinationStationCode is a key in FlightLog and hence it is directly accessible. Same rule is applied on other keys, until any key have period sign, like FlightLog.destinationStationCode, because period have its own meaning (access property on the object). And therefore it is not giving any value (You can verify by replacing period by any other characket, say "_", then all works).

destinationStationCode是FlightLog中的一个键,因此可以直接访问。同样的规则应用于其他键,直到任何键都有周期符号,如FlightLog.destinationStationCode,因为句点有其自己的含义(对象的访问属性)。因此它没有给出任何价值(您可以通过任何其他字符替换句号来验证,说“_”,然后所有工作)。

To access this property, I suggest you to create an field FlightLog of type Object, like

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

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

and access its properties like

并访问其属性,如

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

Hope this helps.

希望这可以帮助。

My files --

我的文件 -

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>