我有一个json字符串,如:
{
"store": {
"book": [
{
"category": "reference",
"authorextra": [
{
"auth1":"Nigel Rees",
"auth2":"xxxx"
}
],
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
我在解析json时使用了DEFAULT_PATH_LEAF_TO_NULL选项。
ReadContext ctx=JsonPath.using(Configuration.builder().options(Option.DEFAULT_PATH_LEAF_TO_NULL).build()).parse(input);
当我读到这条道路时:
returnObj = ctx.read($.store.book[*].authorextra[*].auth1);
我得到的结果如下: [ “Nigel Rees” ]
我期待: [ “Nigel Rees”, 空值, 空值, 空值 ]
我在这里缺少什么配置?任何帮助表示赞赏。 TIA!
答案 0 :(得分:0)
我认为您忽略了DEFAULT_PATH_LEAF_TO_NULL设置的LEAF部分。你的路径有商店 - >书 - > authorextra - > auth1。因此,将设置为NULL的叶子是authorextra缺少的auth1。由于第一本书是唯一一本拥有authorextra的书,它是唯一一本给你结果的书,因为它是你唯一能找到叶子的书。如果你试试这个其他的json文档:
{
"store" : {
"book" : [
{
"category" : "reference",
"authorextra": [
{
"auth1":"Nigel Rees",
"auth2":"xxxx"
}
],
"title" : "Sayings of the Century",
"price" : 8.95
},
{
"category" : "fiction",
"authorextra": [
{
"auth2":"Evelyn Waugh"
}
],
"title" : "Sword of Honour",
"price" : 12.99
},
{
"category" : "fiction",
"author" : "Herman Melville",
"title" : "Moby Dick",
"isbn" : "0-553-21311-3",
"price" : 8.99
},
{
"category" : "fiction",
"author" : "J. R. R. Tolkien",
"title" : "The Lord of the Rings",
"isbn" : "0-395-19395-8",
"price" : 22.99
}
],
"bicycle" : {
"color" : "red",
"price" : 19.95
}
},
"expensive" : 10
}
你会得到[“Nigel Rees”,null],因为第二本书也有authorextra,但缺少auth1,即叶子。