Rom*_*man 2 java rest-assured rest-assured-jsonpath
我需要从 json 文件中获取数据,该文件具有奇怪的结构,但我找不到解决方案。我将restAssured与jsonPath一起使用,当我尝试获取特定周的数据时,如下所示
getJsonPath().getList("years.2017.weeks.find{it.isoWeekNum == '1'}")
Run Code Online (Sandbox Code Playgroud)
我收到错误
Invalid JSON expression: Script1.groovy: 1: unexpected token: 2017 @ line 1, column 33.
years.2017.weeks.find{it.isoWeekNum == '1'}
Run Code Online (Sandbox Code Playgroud)
同时我可以用这个表达式获取数据
getJsonPath().getList("years.2017.weeks")
这会向我返回所有星期的列表。
我发现获得我需要的东西的唯一方法就是这样
getJsonPath().getList("years[1]['2017'].weeks.find{it.isoWeekNum == '1'}")
但这不是我要找的。我需要找到一个解决方案,可以在没有 2017 年 [1] 年和 2016 年 [0] 年的情况下获取数据
"{
"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": []
}
]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
小智 5
希望以下代码正是您所寻找的:
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'}")
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2029 次 |
| 最近记录: |