代码之家  ›  专栏  ›  技术社区  ›  Shruthi

Jayway JsonPath中的DEFAULT\u PATH\u LEAF\u TO\u NULL调整配置不会为内部数组返回NULL

  •  0
  • Shruthi  · 技术社区  · 7 年前

    我有一个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\u PATH\u LEAF\u TO\u NULL选项。

    ReadContext ctx=JsonPath.using(Configuration.builder().options(Option.DEFAULT_PATH_LEAF_TO_NULL).build()).parse(input);
    

    当我阅读此路径时:

    returnObj = ctx.read($.store.book[*].authorextra[*].auth1);
    

    我得到的结果如下: [ “奈杰尔·里斯” ]

    我期待着: [ “奈杰尔·里斯”, 无效的 无效的 无效的 ]

    我缺少什么配置?非常感谢您的帮助。TIA!

    1 回复  |  直到 7 年前
        1
  •  0
  •   Jorge Y.    7 年前

    我认为您忽略了默认的\u PATH\u LEAF\u TO\u NULL设置的叶部分。您的路径具有存储-->书籍-->authorextra-->因此,将设置为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,即叶子。