【问题标题】:RestAssured JsonPath: Issue with getting data from jsonRestAssured JsonPath:从 json 获取数据的问题
【发布时间】:2016-12-08 05:17:29
【问题描述】:

我需要从结构怪异的 json 文件中获取数据,但我找不到解决方案。我正在使用带有 jsonPath 的 restAssured 当我试图像这样获取特定一周的数据时

getJsonPath().getList("years.2017.weeks.find{it.isoWeekNum == '1'}")

我遇到了一个错误

Invalid JSON expression: Script1.groovy: 1: unexpected token: 2017 @ line 1, column 33.
 years.2017.weeks.find{it.isoWeekNum == '1'}

同时我可以用这个表达式获取数据

getJsonPath().getList("years.2017.weeks")

返回给我的所有周列表。

我发现得到我需要的唯一方法就是这样

getJsonPath().getList("years[1]['2017'].weeks.find{it.isoWeekNum == '1'}")

但这不是我要找的。我需要找到一个解决方案,我可以在没有年份 [1] 的 2017 年和年份 [0] 的 2016 年获取数据

 "{
  "years": [
    {
      "2016": {
        "currentIsoWeek": "49",
        "currentTourWeek": "49",
        "weeks": [
          {
            "isoWeekNum": "1",
            "tourWeekNum": "1",
            "categories": []
          },
          {
            "isoWeekNum": "2",
            "tourWeekNum": "2",
            "categories": []
          }
        ]
      }
    },
    {
      "2017": {
        "currentIsoWeek": "",
        "currentTourWeek": "",
        "weeks": [
          {
            "isoWeekNum": "1",
            "tourWeekNum": "1",
            "categories": []
          },
          {
            "isoWeekNum": "2",
            "tourWeekNum": "2",
            "categories": []
          }
        ]
      }
    }
  ]
}

【问题讨论】:

    标签: java rest-assured rest-assured-jsonpath


    【解决方案1】:

    希望以下代码正是您要找的:

    public static void main(final String[] args) {
        JsonPath jsonPath = new JsonPath(yourJson).using(new JsonPathConfig("UTF-8"));
    
        System.out.println(
                jsonPath.get("years['2017'].weeks*.find {it.isoWeekNum == '1'}")
        );
    }
    

    【讨论】:

    • 非常感谢我在这个问题上研究了很长时间,终于找到了解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    相关资源
    最近更新 更多