【发布时间】:2017-09-15 21:43:58
【问题描述】:
使用java json路径库:https://github.com/json-path/JsonPath
给定一个这样的 json
{
"store": {
"book": [
{
"category": "reference"
},
{
"category": "fiction"
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
我想将“book”作为字符串列表返回。例如:
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的子类型,在这种情况下恰好包含字符串。 -
由于 book 是 json 树中的 JsonArray,您可以 JsonArray tupe 并从中检索数据并将其添加到列表中。