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

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

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);
// ...
}

英文:
JsonPath jsonPath = response.jsonPath();
List&lt;String&gt; listOfProducts= jsonPath.getList(&quot;products.stock.availabilities.status&quot;);

 for (int i = 0; i &lt; 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:

 &quot;products&quot;: [
        {
            &quot;id&quot;: &quot;1&quot;,
            &quot;product&quot;: {
                &quot;color&quot;: &quot;blue&quot;,
				&quot;brand&quot;: &quot;Brand&quot;
               
            },
            &quot;stock&quot;: {
                &quot;availabilities&quot;: [
                    {
                        &quot;status&quot;: &quot;IN_STOCK&quot;
                    }
                ]
            },
            &quot;price&quot;: {
                &quot;total&quot;: &quot;1000&quot;,
                &quot;currency&quot;: &quot;PLN&quot;
            }
        },
		{
		..
		}
		]

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(
    &quot;{&quot; +
    &quot;    \&quot;products\&quot;: [&quot; +
    &quot;        {&quot; +
    &quot;            \&quot;id\&quot;: \&quot;1\&quot;,&quot; +
    &quot;            \&quot;product\&quot;: {&quot; +
    &quot;                \&quot;color\&quot;: \&quot;blue\&quot;,&quot; +
    &quot;                \&quot;brand\&quot;: \&quot;Brand\&quot;&quot; +
    &quot;            },&quot; +
    &quot;            \&quot;stock\&quot;: {&quot; +
    &quot;                \&quot;availabilities\&quot;: [&quot; +
    &quot;                    {&quot; +
    &quot;                        \&quot;status\&quot;: \&quot;IN_STOCK\&quot;&quot; +
    &quot;                    }&quot; +
    &quot;                ]&quot; +
    &quot;            },&quot; +
    &quot;            \&quot;price\&quot;: {&quot; +
    &quot;                \&quot;total\&quot;: \&quot;1000\&quot;,&quot; +
    &quot;                \&quot;currency\&quot;: \&quot;PLN\&quot;&quot; +
    &quot;            }&quot; +
    &quot;        }&quot; +
    &quot;    ]&quot; +
    &quot;}&quot;);

Query to Produce JSON Array Node

String expression = &quot;products.stock.availabilities.status&quot;;
JsonNode array = josson.getNode(expression);
System.out.println(expression);
System.out.println(array.toPrettyString());
for (int i = 0; i &lt; array.size(); i++) {
    System.out.println(array.get(i).asText());
}

expression = &quot;products.product.color&quot;;
array = josson.getNode(expression);
System.out.println(expression);
System.out.println(array.toPrettyString());
for (int i = 0; i &lt; array.size(); i++) {
    System.out.println(array.get(i).asText());
}

Output

products.stock.availabilities.status
[ &quot;IN_STOCK&quot; ]
IN_STOCK
products.product.color
[ &quot;blue&quot; ]
blue

答案2

得分: 0

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

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

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;

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:

List<List<String>> listOfProducts = jsonPath.getList("products.stock.availabilities.status");

for (List<String> statuses : listOfProducts) {
    for (String actualProduct : statuses) {
        ...
    }
}
英文:

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:

List&lt;List&lt;String&gt;&gt; listOfProducts = jsonPath.getList(&quot;products.stock.availabilities.status&quot;);

for (List&lt;String&gt; 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(&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:

List&lt;List&lt;String&gt;&gt; listOfProducts = jsonPath.getList(&quot;products.stock.availabilities.status&quot;);

List&lt;String&gt; allStatuses = new ArrayList&lt;&gt;();
for (List&lt;String&gt; productStatuses : listOfProducts) {
    allStatuses.addAll(productStatuses);
}

for (String status : allStatuses) {
    // process ..
}

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:

确定