【发布时间】:2016-07-19 03:46:35
【问题描述】:
问题:
我有一个接受JSON 字符串作为输入的服务。 JSON 架构在某些字段并不总是存在的情况下每次都会有所不同。当它们存在时,如何使用 Jayway 的 JsonPath 查询这些字段的值?
我的尝试:
我使用 Option.DEFAULT_PATH_LEAF_TO_NULL 作为 Jayway 的 readme page 解释
Configuration config = Configuration.defaultConfiguration()
.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
if (JsonPath.isPathDefinite(attribute.jsonPath))
{
String value = JsonPath.using(config).parse(currentTx).read(attribute.jsonPath);
if (value != null)
{
attributeValues.add(value);
}
}
else
{
List<String> attributeValuesArray = JsonPath.using(config).parse(currentTx).read(attribute.jsonPath);
for (String value : attributeValuesArray)
{
if (value != null)
{
attributeValues.add(value);
}
}
}
如果找不到路径,这应该使JsonPath.read() 返回null,但是我的代码仍然抛出:
com.jayway.jsonpath.PathNotFoundException:路径 $['somepath'] 中缺少属性
当我给它一条不存在的路径时。有谁知道这可能是什么原因造成的?
【问题讨论】: