2017-09-25 85 views
1

这是我的测试数据:如何指示特定的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 
     } 
     ] 
    } 
    ] 
} 

我试图去使用此路径的Timeoff类型描述:

$.responseItems[0].timeOffStaffSummaryDTO[0].timeOffType.description 

现在,这适用于任何JSON路径测试我在网上尝试,但该模板不会在页面标题栏中显示此值。

在重复细节带,我也试图显示此:

$.responseItems.timeOffStaffSummaryDTO[0].timeOffTypeWithoutAllowance 

试了一下没有$符号或第一点,没有什么区别,它总是显示为空。我也尝试过在字段描述中或作为属性“net.sf.jasperreports.json.field.expression”的值。

我错过了什么?

回答

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需求相对于你的 数据源(的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> 

输出

result

+0

许多,非常感谢 - 特别是花时间显示代码和输出!我的查询定义是正确的,但我的字段定义已关闭 - 我需要摆脱.responseItems位。使用JSONPath非常新,直到现在才处理xml数据。 – user2668539

+0

@ user2668539您的欢迎,感谢您的接受 –