这是我的测试数据:
{
"errorCode": null,
"errorMessage": null,
"responseItems": [
{
"errorCode": null,
"errorMessage": null,
"personId": "FCC2",
"personCode": "SUNEETHA",
"personFirstName": "suneetha",
"personLastName": "Durgam",
"office": "London",
"officeCode": "L",
"department": "Product",
"departmentCode": "PR",
"subDepartment": "QA",
"subDepartmentCode": "QA",
"timeOffStaffSummaryDTO": [
{
"officeCode": null,
"startMonthYear": null,
"endMonthYear": null,
"personId": null,
"alphaId": null,
"continent": null,
"allowancesIntotal": 20,
"allowancesUsed": 0,
"allowancesSubmitted": 0,
"allowancesApproved": 0,
"allowancesRemaining": 20,
"timeOffType": {
"id": 4001,
"continent": "EU",
"alphaId": "9J",
"code": "PTO",
"description": "Personal Time Offdd",
"account": "MADMIN",
"colourCode": "#CCCCCC",
"approvalRequired": true,
"commentRequired": false,
"paid": true
},
"timeOffTypeWithoutAllowance": false
}
]
}
]
}
我正在尝试使用此路径进入Timeoff类型描述:
$.responseItems[0].timeOffStaffSummaryDTO[0].timeOffType.description
现在,这适用于我在线试过的任何json路径测试器,但模板不会在页眉带中显示此值。
在重复的细节乐队中,我也试图显示:
$.responseItems.timeOffStaffSummaryDTO[0].timeOffTypeWithoutAllowance
尝试没有$符号或第一个点,没有区别,它总是显示为null。我也在字段描述中尝试过它,或者作为属性值#34; net.sf.jasperreports.json.field.expression"。
我错过了什么?
答案 0 :(得分:2)
您在使用json时缺少如何声明字段,我将向您展示一个考虑您的json的示例。
如果数据源的基本路径(queryString,报告将迭代这些项目上的详细信息区段)为responseItems
<queryString language="JSON">
<![CDATA[responseItems]]>
</queryString>
要访问personCode
,您需要将字段定义为
<field name="personCode" class="java.lang.String">
<fieldDescription><![CDATA[personCode]]></fieldDescription>
</field>
fieldDescription 是json中的路径,因此要访问timeOffStaffSummaryDTO[0].timeOffType.description
您需要声明一个字段
<field name="timeOffStaffSummaryDTOTimeOffTypeDescription" class="java.lang.String">
<fieldDescription><![CDATA[timeOffStaffSummaryDTO[0].timeOffType.description]]></fieldDescription>
</field>
名称可以是您喜欢的任何名称,该类是相应的java 值的类和
fieldDescription
需要是相对于您的路径 datasource(queryString)。
完整示例
标题(粗体)和详细信息带
中的timeOffStaffSummaryDTO[0].timeOffType.description
的完整示例
<?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="jsonTest" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="afa1e750-acfe-4d43-92ff-76e139d34dac">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="JsonTest"/>
<queryString language="JSON">
<![CDATA[responseItems]]>
</queryString>
<field name="personCode" class="java.lang.String">
<fieldDescription><![CDATA[personCode]]></fieldDescription>
</field>
<field name="timeOffStaffSummaryDTOTimeOffTypeDescription" class="java.lang.String">
<fieldDescription><![CDATA[timeOffStaffSummaryDTO[0].timeOffType.description]]></fieldDescription>
</field>
<pageHeader>
<band height="35" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="260" height="30" uuid="449b7c85-a952-4205-9595-de2647d563ed"/>
<textElement verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{timeOffStaffSummaryDTOTimeOffTypeDescription}]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<detail>
<band height="32" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="180" height="30" uuid="832d525e-b932-4563-9f00-c4e3fc671061"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{personCode}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="180" y="0" width="260" height="30" uuid="528a9d25-2329-4f1f-b0be-21d1b6b8a5a0"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{timeOffStaffSummaryDTOTimeOffTypeDescription}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
<强>输出强>