如何指明特定的JSON路径?

How to indicate a specific JSON path?

这是我的测试数据:

{
  "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。我也在字段描述中尝试过它或作为 属性 "net.sf.jasperreports.json.field.expression" 的值。

我错过了什么?

您不知道如何在使用 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>

The name can be whatever you like, the class is the corresponding java class of the value and the fieldDescription needs to be the path relative to your datasource (queryString).

完整示例

header(粗体)和详细带

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>

输出