使用Java JSON路徑庫:https://github.com/json-path/JsonPath返回的Java JsonPath作爲一個JSON字符串
給出一個JSON這樣
{
"store": {
"book": [
{
"category": "reference"
},
{
"category": "fiction"
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
我想返回「書」作爲一個字符串列表。對於例子:
List<String> results = JsonPath.read(example, "$.store.book[*]");
和結果應該是這樣的:
["{\"category\":\"reference\"}", "{\"category\":\"fiction\"}"]
有沒有辦法來實現這一目標?
目前:
System.out.println(udf.jsonExtractScalar(testExample, "$.store.book[*]").getClass().getName());
--> net.minidev.json.JSONArray
有什麼問題的課嗎? 'JSONArray'是'List'的一個子類型,在這種情況下恰好包含了Strings。 –
由於book是JSON樹中的JsonArray,因此您可以JsonArray tupe並從中檢索數據並將其添加到列表中。 –