[英]Exception in thread "main" java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class java.util.List
When working with REST Assured Java framework, I get the following error.使用 REST Assured Java 框架时,出现以下错误。
Exception in thread "main" java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class java.util.List (java.util.LinkedHashMap and java.util.List are in module java.base of loader 'bootstrap')
at io.restassured.path.json.JsonPath.getList(JsonPath.java:375)
at qaflightsuppliers.QaFlightDXBLONResponseDebugValidation.main(QaFlightDXBLONResponseDebugValidation.java:94)
Below is my code.下面是我的代码。 Please correct my code.
请更正我的代码。
List<String> jsonMapStringObjectResponse = response.jsonPath().get("recommendation");
for (int i = 0; i <= jsonMapStringObjectResponse.size(); i++)
{
List<String> jsonMapObjectResponse = response.jsonPath().getList("recommendation[" + i + "]");
System.out.print("ValidatingAirlineName="+jsonMapObjectResponse.add("validatingAirlineName"));
}
You are trying to read the attribute recommendation
as a List
, but in reality in the JSON response it's a Map
.您正在尝试将属性
recommendation
读取为List
,但实际上在 JSON 响应中它是Map
。
So you have to change:所以你必须改变:
List<String> jsonMapObjectResponse = response.jsonPath().getList("recommendation[" + i + "]");
to至
Map<String, String> jsonMapObjectResponse = response.jsonPath().getMap("recommendation[" + i + "]");
// example below
System.out.println(jsonMapObjectResponse.get("validatingAirlineName"));
Reverse engineered from this question your JSON probably looks something like this:从这个问题进行逆向工程,您的 JSON 可能看起来像这样:
[
{
"id": 1,
"recommendation": {
"validatingAirlineName": "...",
"adultBaseFare": "...",
"adultTaxFare": "..."
}
},
{
"id": 2,
"recommendation": {
"validatingAirlineName": "...",
"adultBaseFare": "...",
"adultTaxFare": "..."
}
}
]
Tested and verified with this demo, which might be a good reading resource as well:使用此演示进行了测试和验证,这也可能是一个很好的阅读资源:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.