如何指示具体的JSON路径? [英] How to indicate a specific JSON path?

查看:36
本文介绍了如何指示具体的JSON路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的测试数据:

{
  "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
        }
      ]
    }
  ]
}

我正在尝试使用以下路径访问休假类型描述:

$.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>
名称可以是您喜欢的任何名称,类是对应的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>

输出

这篇关于如何指示具体的JSON路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆