英文:
jsonPath().getList returns ArrayList - how to convert to List<String>?
问题
JsonPath jsonPath = response.jsonPath();
List
for (int i = 0; i < listOfProducts.size(); i++) {
String actualProduct = listOfProducts.get(i);
// ...
}
英文:
JsonPath jsonPath = response.jsonPath();
List<String> listOfProducts= jsonPath.getList("products.stock.availabilities.status");
for (int i = 0; i < listOfProducts.size(); i++) {
String actualProduct = listOfProducts.get(i);
...
}
throws class java.util.ArrayList cannot be cast to class java.lang.String
response in this case is:
"products": [
{
"id": "1",
"product": {
"color": "blue",
"brand": "Brand"
},
"stock": {
"availabilities": [
{
"status": "IN_STOCK"
}
]
},
"price": {
"total": "1000",
"currency": "PLN"
}
},
{
..
}
]
For other JSONs it works (where the listOfProducts is actually a List<String>) but for this one I don't know why it's ArrayList.
How to either convert the arrayList to List<String> or how to servce both (List<String> and ArrayList<String> depending on the json response)?
EDIT:
I want to have this method common for ArrayLists and for List<String>
The products.stock.availabilities.status
will be a parameter
答案1
得分: 0
You may try another JSON library Josson.
https://github.com/octomix/josson
Deserialization
Josson josson = Josson.fromJsonString(
"{
"products": [
{
"id": "1",
"product": {
"color": "blue",
"brand": "Brand"
},
"stock": {
"availabilities": [
{
"status": "IN_STOCK"
}
]
},
"price": {
"total": "1000",
"currency": "PLN"
}
}
]
}"
);
Query to Produce JSON Array Node
String expression = "products.stock.availabilities.status";
JsonNode array = josson.getNode(expression);
System.out.println(expression);
System.out.println(array.toPrettyString());
for (int i = 0; i < array.size(); i++) {
System.out.println(array.get(i).asText());
}
expression = "products.product.color";
array = josson.getNode(expression);
System.out.println(expression);
System.out.println(array.toPrettyString());
for (int i = 0; i < array.size(); i++) {
System.out.println(array.get(i).asText());
}
Output
products.stock.availabilities.status
[ "IN_STOCK" ]
IN_STOCK
products.product.color
[ "blue" ]
blue
英文:
You may try another JSON library Josson.
https://github.com/octomix/josson
Deserialization
Josson josson = Josson.fromJsonString(
"{" +
" \"products\": [" +
" {" +
" \"id\": \"1\"," +
" \"product\": {" +
" \"color\": \"blue\"," +
" \"brand\": \"Brand\"" +
" }," +
" \"stock\": {" +
" \"availabilities\": [" +
" {" +
" \"status\": \"IN_STOCK\"" +
" }" +
" ]" +
" }," +
" \"price\": {" +
" \"total\": \"1000\"," +
" \"currency\": \"PLN\"" +
" }" +
" }" +
" ]" +
"}");
Query to Produce JSON Array Node
String expression = "products.stock.availabilities.status";
JsonNode array = josson.getNode(expression);
System.out.println(expression);
System.out.println(array.toPrettyString());
for (int i = 0; i < array.size(); i++) {
System.out.println(array.get(i).asText());
}
expression = "products.product.color";
array = josson.getNode(expression);
System.out.println(expression);
System.out.println(array.toPrettyString());
for (int i = 0; i < array.size(); i++) {
System.out.println(array.get(i).asText());
}
Output
products.stock.availabilities.status
[ "IN_STOCK" ]
IN_STOCK
products.product.color
[ "blue" ]
blue
答案2
得分: 0
大家都提到了正确的根本原因,并提供了一个很好的解决方案,但这不是你期望的,也许你想要一个更短的解决方案,代码更少,不再需要循环。这是你要的:
flatten()
方法将帮助将 List<List<String>
转换为 List<String>
List<String> listOfProducts= JsonPath.from(json).getList("products.stock.availabilities.status.flatten()");
英文:
Everyone stated the correct root cause and provided a good solution for it, but that was not what you expect, maybe you want a shorter solution with less code and no more loop. Here you are:
flatten()
method will help to convert List<List<String>
to List<String>
List<String> listOfProducts= JsonPath.from(json).getList("products.stock.availabilities.status.flatten()");
答案3
得分: -1
Your call jsonPath.getList("products.stock.availabilities.status");
will return a List<List<String>>
: Both products
and availabilities
are Lists. So:
List<List<String>> listOfProducts = jsonPath.getList("products.stock.availabilities.status");
for (List<String> statuses : listOfProducts) {
for (String actualProduct : statuses) {
...
}
}
英文:
Your call jsonPath.getList("products.stock.availabilities.status");
will return a List<List<String>>
: Both products
and availabilities
are Lists. So:
List<List<String>> listOfProducts = jsonPath.getList("products.stock.availabilities.status");
for (List<String> statuses : listOfProducts) {
for (String actualProduct : statuses) {
...
}
}
答案4
得分: -1
你的status
字段位于availabilities
数组中,因此当你调用getList("products.stock.availabilities.status")
时,实际上获取的是一个数组列表
你可以通过将所有状态收集到一个列表中来展开这个结构,例如:
List<List<String>> listOfProducts = jsonPath.getList("products.stock.availabilities.status");
List<String> allStatuses = new ArrayList<>();
for (List<String> productStatuses : listOfProducts) {
allStatuses.addAll(productStatuses);
}
for (String status : allStatuses) {
// 处理 ..
}
英文:
Your status
field is within an availabilities
array, so when you're calling getList("products.stock.availabilities.status")
, you're actually getting a list of arrays
You can flatten this structure by collecting all statuses in one list, like:
List<List<String>> listOfProducts = jsonPath.getList("products.stock.availabilities.status");
List<String> allStatuses = new ArrayList<>();
for (List<String> productStatuses : listOfProducts) {
allStatuses.addAll(productStatuses);
}
for (String status : allStatuses) {
// process ..
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论