如何解决"无效表达,当前令牌......未正确结束"错误?

时间:2017-11-24 11:46:57

标签: json jasper-reports

这是我的数据

{
  "errorCode": null,
  "errorMessage": null,
  "responseItems": [
    {
      "personFirstName": "t16239d",
      "personLastName": "submitter 1",
      "hireDate": "20161106",
      "terminationDate": null,
      "startMonthYear": "01_2017",
      "endMonthYear": "12_2017",
      "staffTimeOffAllowances": [
        {
          "officeCode": null,
          "startMonthYear": null,
          "endMonthYear": null,
          "personId": null,
          "personCode": null,
          "allowancesIntotal": 10,
          "allowancesUsed": 0,
          "allowancesSubmitted": 0,
          "allowancesApproved": 0,
          "allowancesRemaining": 10,
          "timeOffType": {
            "id": 1284,
            "continent": "EU",
            "alphaId": "CA",
            "code": "SUM1",
            "description": "summer friday1",
            "account": "ADMIN1",
            "colourCode": "#CCCCCC",
            "visibility": "R",
            "approvalRequired": true,
            "commentRequired": false,
            "paid": true
          },
          "timeOffTypeWithoutAllowance": false,
          "proRataAllowanceData": false
        },
        {
          "officeCode": null,
          "startMonthYear": null,
          "endMonthYear": null,
          "personId": null,
          "personCode": null,
          "allowancesIntotal": 0,
          "allowancesUsed": 3,
          "allowancesSubmitted": 0,
          "allowancesApproved": 0,
          "allowancesRemaining": 3,
          "timeOffType": {
            "id": 1342,
            "continent": "EU",
            "alphaId": "CA",
            "code": "SICK",
            "description": "Sickness",
            "account": "SICK",
            "colourCode": "#CCCCCC",
            "visibility": "R",
            "approvalRequired": true,
            "commentRequired": false,
            "paid": true
          },
          "timeOffTypeWithoutAllowance": true,
          "proRataAllowanceData": false
        }
      ]
    }
  ]
}

我正在尝试提取具有allowancesUsed的第一个staffTimeOffAllowances条目的timeOffTypeWithoutAllowance = true字段的值。

当前的尝试是:

staffTimeOffAllowances[?(@.timeOffTypeWithoutAllowance==true)].allowancesUsed[0]

它编译得很好但是当我尝试预览它时,它失败了

  

表达式无效,当前令牌staffTimeOffAlowances [?(@未正确结束

我尝试了很多相同表达的变化,但没有快乐。有人可以告诉我我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

基本json查询语言需要绝对表达式。我建议将您的查询停在比您想要的JSON属性高一级的位置,以便您可以将它用作字段表达式(使用" leaf"值需要一个特殊的字段表达式 - "." - 并且可能会产生不可预知的结果)。话虽如此,你应该对这个查询和字段映射没问题:

<queryString language="json">
    <![CDATA[responseItems.staffTimeOffAllowances(timeOffTypeWithoutAllowance == true)[0]]]>
</queryString>
<field name="allowancesUsed" class="java.lang.Integer"/>

从JasperReports 6.3.1开始,您可以使用更灵活/高级的jsonql语言。只需将上面的queryString声明替换为:

  • 这一个:

    <queryString language="jsonql">
        <![CDATA[..staffTimeOffAllowances.*(timeOffTypeWithoutAllowance == true)[0]]]>
    </queryString>
    

    此查询转换为:&#34;从任意位置获取staffTimeOffAllowances属性,获取具有timeOffTypeWithoutAllowance的子项,选择第一个&#34;

  • 或者这个:

    <queryString language="jsonql">
        <![CDATA[..timeOffTypeWithoutAllowance(@val == true)^[0]]]>
    </queryString>
    

    此查询转换为:&#34;从任意位置获取带有 true 值的timeOffTypeWithoutAllowance属性,获取其父项,选择第一个&#34;

    < / LI>

重要说明jsonjsonql是与JsonPath无关的自定义查询语言,尽管有些相似之处。 json是第一个(并且非常简单化)尝试提供查询JSON结构的方法,其后是jsonql,它旨在替换json,因为它提供了更多功能并且更具可预测性。