Jayway针对对象和数组的JSONPath表达式

46scxncf  于 11个月前  发布在  其他
关注(0)|答案(3)|浏览(126)

如果父元素可以是JSONArray以及JSONObject,我不得不麻烦检索值。为此,我使用Deepcopy语法来检索它。现在的问题是,如果子属性也存在于Inner Array中,我将获得额外的值。
例如:JpathExpression:

$.store..book..innerBook..category

字符串
结果是:

[
   "innerReference1",
   "innerBook1Ref1",
   "innerReference2"
]


实施例1预期结果为:

[
   "innerReference1",
   "innerReference2"
]


实施例1:

{
    "store": {
        "book": [
        {
                "innerBook": [
                    {
                        "category": "innerReference1",
                        "author": "Nigel Rees",
                        "innerBook1": [
                            {
                                "category": "innerBook1Ref1"
                            }
                        ]
                    },
                    {
                        "category": "innerReference2",
                        "author": "Nigel Rees"
                    }
                ]
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}


实施例2:

{
    "store": {
        "book": [
        {
                "innerBook": 
                    {
                        "category": "innerReference1",
                        "author": "Nigel Rees",
                        "innerBook1": [
                            {
                                "category": "innerBook1Ref1"
                            }
                        ]
                    }
                
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}


实施例2预期结果为:

[
   "innerReference1"
]

093gszye

093gszye1#

但是,我们不需要在innerBook之后使用递归下降..来遍历数组中的元素;使用Jayway的方法,我们可以使用innerBook[*].category来检索所需的结果,例如1:

$.store..book..innerBook[*].category

字符串
示例2要求删除数组访问器:

$.store..book..innerBook.category


你不能让innerBook在一个JSON中作为数组和对象,因为它无效。
如果你有一个API(或其他),它为JSON提供了一个数组或对象类型的innerbook属性(这将是一个设计糟糕的API,因为API的全部目的就是要有预期的响应),你可以查询两个路径,或者根据innerBook1的存在来访问类别:

$.store.book..innerBook[?(@.innerBook1)]..category


它只为两个示例返回

[
   "innerReference1",
   "innerBook1Ref1"
]


在线here

uqdfh47h

uqdfh47h2#

也许下面是一个更简单的例子。

[
  {
    "title": "The Doe",
    "author": {
      "name": "John Doe"
    }
  },
  {
    "title": "The Does",
    "author": [
      {
        "name": "John Doe"
      },
      {
        "name": "Jane Doe"
      }
    ]
  }
]

字符串
我相信我们正在寻找的JSON路径可以返回所有作者的名字,而无需对name进行深度扫描(即..name)。

qcuzuvrc

qcuzuvrc3#

JSONPath似乎无法在对象和数组上使用相同的表达式返回预期结果。您可以尝试另一个JSON库 Josson,因为它使用相同的表达式处理对象和数组。
https://github.com/octomix/josson

示例1

Josson ex1 = Josson.fromJsonString(
    "{" +
    "    \"store\": {" +
    "        \"book\": [" +
    "        {" +
    "                \"innerBook\": [" +
    "                    {" +
    "                        \"category\": \"innerReference1\"," +
    "                        \"author\": \"Nigel Rees\"," +
    "                        \"innerBook1\": [" +
    "                            {" +
    "                                \"category\": \"innerBook1Ref1\"" +
    "                            }" +
    "                        ]" +
    "                    }," +
    "                    {" +
    "                        \"category\": \"innerReference2\"," +
    "                        \"author\": \"Nigel Rees\"" +
    "                    }" +
    "                ]" +
    "            }" +
    "        ]," +
    "        \"bicycle\": {" +
    "            \"color\": \"red\"," +
    "            \"price\": 19.95" +
    "        }" +
    "    }," +
    "    \"expensive\": 10" +
    "}");

字符串

  • 查询 *
JsonNode node = ex1.getNode("store.book.innerBook.category");
System.out.println(node.toPrettyString());
//Output: [ "innerReference1", "innerReference2" ]

实施例2

Josson ex2 = Josson.fromJsonString(
    "{" +
    "    \"store\": {" +
    "        \"book\": [" +
    "        {" +
    "                \"innerBook\": " +
    "                    {" +
    "                        \"category\": \"innerReference1\"," +
    "                        \"author\": \"Nigel Rees\"," +
    "                        \"innerBook1\": [" +
    "                            {" +
    "                                \"category\": \"innerBook1Ref1\"" +
    "                            }" +
    "                        ]" +
    "                    }" +
    "                " +
    "            }" +
    "        ]," +
    "        \"bicycle\": {" +
    "            \"color\": \"red\"," +
    "            \"price\": 19.95" +
    "        }" +
    "    }," +
    "    \"expensive\": 10" +
    "}");

  • 查询 *
JsonNode node = ex2.getNode("store.book.innerBook.category");
System.out.println(node.toPrettyString());
//Output: [ "innerReference1" ]

实施例3

由@thalhamm建议

Josson ex3 = Josson.fromJsonString(
    "[" +
    "  {" +
    "    \"title\": \"The Doe\"," +
    "    \"author\": {" +
    "      \"name\": \"John Doe\"" +
    "    }" +
    "  }," +
    "  {" +
    "    \"title\": \"The Does\"," +
    "    \"author\": [" +
    "      {" +
    "        \"name\": \"John Doe\"" +
    "      }," +
    "      {" +
    "        \"name\": \"Jane Doe\"" +
    "      }" +
    "    ]" +
    "  }" +
    "]");

  • 查询 *
JsonNode node = ex3.getNode("author.name");
System.out.println(node.toPrettyString());
//Output: [ "John Doe", "John Doe", "Jane Doe" ]

相关问题