DEFAULT_PATH_LEAF_TO_NULL 调整 Jayway JsonPath 中的配置不会 return 内部数组为 null

DEFAULT_PATH_LEAF_TO_NULL Tweaking Configuration in Jayway JsonPath doesnot return null for inner arrays

我有一个 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 !

我认为您忽略了 DEFAULT_PATH_LEAF_TO_NULL 设置的 LEAF 部分。你的路径有 store --> book --> 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,也就是叶子。