jsonPath().getList 返回 ArrayList – 如何转换为 List

huangapple go评论124阅读模式
英文:

jsonPath().getList returns ArrayList - how to convert to List<String>?

问题

JsonPath jsonPath = response.jsonPath();
List listOfProducts = jsonPath.getList("products.stock.availabilities.status", String.class);

for (int i = 0; i < listOfProducts.size(); i++) {
String actualProduct = listOfProducts.get(i);
// ...
}

英文:
  1. JsonPath jsonPath = response.jsonPath();
  2. List&lt;String&gt; listOfProducts= jsonPath.getList(&quot;products.stock.availabilities.status&quot;);
  3. for (int i = 0; i &lt; listOfProducts.size(); i++) {
  4. String actualProduct = listOfProducts.get(i);
  5. ...
  6. }

throws class java.util.ArrayList cannot be cast to class java.lang.String

response in this case is:

  1. &quot;products&quot;: [
  2. {
  3. &quot;id&quot;: &quot;1&quot;,
  4. &quot;product&quot;: {
  5. &quot;color&quot;: &quot;blue&quot;,
  6. &quot;brand&quot;: &quot;Brand&quot;
  7. },
  8. &quot;stock&quot;: {
  9. &quot;availabilities&quot;: [
  10. {
  11. &quot;status&quot;: &quot;IN_STOCK&quot;
  12. }
  13. ]
  14. },
  15. &quot;price&quot;: {
  16. &quot;total&quot;: &quot;1000&quot;,
  17. &quot;currency&quot;: &quot;PLN&quot;
  18. }
  19. },
  20. {
  21. ..
  22. }
  23. ]

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

  1. Josson josson = Josson.fromJsonString(
  2. &quot;{&quot; +
  3. &quot; \&quot;products\&quot;: [&quot; +
  4. &quot; {&quot; +
  5. &quot; \&quot;id\&quot;: \&quot;1\&quot;,&quot; +
  6. &quot; \&quot;product\&quot;: {&quot; +
  7. &quot; \&quot;color\&quot;: \&quot;blue\&quot;,&quot; +
  8. &quot; \&quot;brand\&quot;: \&quot;Brand\&quot;&quot; +
  9. &quot; },&quot; +
  10. &quot; \&quot;stock\&quot;: {&quot; +
  11. &quot; \&quot;availabilities\&quot;: [&quot; +
  12. &quot; {&quot; +
  13. &quot; \&quot;status\&quot;: \&quot;IN_STOCK\&quot;&quot; +
  14. &quot; }&quot; +
  15. &quot; ]&quot; +
  16. &quot; },&quot; +
  17. &quot; \&quot;price\&quot;: {&quot; +
  18. &quot; \&quot;total\&quot;: \&quot;1000\&quot;,&quot; +
  19. &quot; \&quot;currency\&quot;: \&quot;PLN\&quot;&quot; +
  20. &quot; }&quot; +
  21. &quot; }&quot; +
  22. &quot; ]&quot; +
  23. &quot;}&quot;);

Query to Produce JSON Array Node

  1. String expression = &quot;products.stock.availabilities.status&quot;;
  2. JsonNode array = josson.getNode(expression);
  3. System.out.println(expression);
  4. System.out.println(array.toPrettyString());
  5. for (int i = 0; i &lt; array.size(); i++) {
  6. System.out.println(array.get(i).asText());
  7. }
  8. expression = &quot;products.product.color&quot;;
  9. array = josson.getNode(expression);
  10. System.out.println(expression);
  11. System.out.println(array.toPrettyString());
  12. for (int i = 0; i &lt; array.size(); i++) {
  13. System.out.println(array.get(i).asText());
  14. }

Output

  1. products.stock.availabilities.status
  2. [ &quot;IN_STOCK&quot; ]
  3. IN_STOCK
  4. products.product.color
  5. [ &quot;blue&quot; ]
  6. blue

答案2

得分: 0

大家都提到了正确的根本原因,并提供了一个很好的解决方案,但这不是你期望的,也许你想要一个更短的解决方案,代码更少,不再需要循环。这是你要的:

flatten() 方法将帮助将 List&lt;List&lt;String&gt; 转换为 List&lt;String&gt;

  1. List&lt;String&gt; listOfProducts= JsonPath.from(json).getList(&quot;products.stock.availabilities.status.flatten()&quot;);
英文:

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&lt;List&lt;String&gt; to List&lt;String&gt;

  1. List&lt;String&gt; listOfProducts= JsonPath.from(json).getList(&quot;products.stock.availabilities.status.flatten()&quot;);

答案3

得分: -1

Your call jsonPath.getList("products.stock.availabilities.status"); will return a List<List<String>>: Both products and availabilities are Lists. So:

  1. List<List<String>> listOfProducts = jsonPath.getList("products.stock.availabilities.status");
  2. for (List<String> statuses : listOfProducts) {
  3. for (String actualProduct : statuses) {
  4. ...
  5. }
  6. }
英文:

Your call jsonPath.getList(&quot;products.stock.availabilities.status&quot;); will return a List&lt;List&lt;String&gt;&gt;: Both products and availabilities are Lists. So:

  1. List&lt;List&lt;String&gt;&gt; listOfProducts = jsonPath.getList(&quot;products.stock.availabilities.status&quot;);
  2. for (List&lt;String&gt; statuses : listOfProducts) {
  3. for (String actualProduct : statuses) {
  4. ...
  5. }
  6. }

答案4

得分: -1

你的status字段位于availabilities数组中,因此当你调用getList("products.stock.availabilities.status")时,实际上获取的是一个数组列表

你可以通过将所有状态收集到一个列表中来展开这个结构,例如:

  1. List<List<String>> listOfProducts = jsonPath.getList("products.stock.availabilities.status");
  2. List<String> allStatuses = new ArrayList<>();
  3. for (List<String> productStatuses : listOfProducts) {
  4. allStatuses.addAll(productStatuses);
  5. }
  6. for (String status : allStatuses) {
  7. // 处理 ..
  8. }
英文:

Your status field is within an availabilities array, so when you're calling getList(&quot;products.stock.availabilities.status&quot;), you're actually getting a list of arrays

You can flatten this structure by collecting all statuses in one list, like:

  1. List&lt;List&lt;String&gt;&gt; listOfProducts = jsonPath.getList(&quot;products.stock.availabilities.status&quot;);
  2. List&lt;String&gt; allStatuses = new ArrayList&lt;&gt;();
  3. for (List&lt;String&gt; productStatuses : listOfProducts) {
  4. allStatuses.addAll(productStatuses);
  5. }
  6. for (String status : allStatuses) {
  7. // process ..
  8. }

huangapple
  • 本文由 发表于 2023年6月26日 21:28:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定