JsonPath 中无缘无故的预期字符
Expected Character for no reason in a JsonPath
假设我有这个 JSON:
{
"response" : {
"code" : "XXX",
"label" : "Lorem Ipsum",
"items" : [
{
"code" : "200",
"label" : "200 !!!"
},
{
"code" : "300",
"label" : "300 !!!!!"
},
{
"code" : "500",
"label" : "+500 !!!!!"
}]
}
}
我想在 Java 中 code
= 500(例如)时获取项目的标签。
我正在使用 jayWay
库和这个 jsonPath:
"$.response.items[?(@.code='500')].label"
我在解析时遇到此错误:Expected character: )
java代码:
public static String getElementValueByJsonPath(String jsonContent, String jsonPath) {
if (checkJsonValidity(jsonContent)) {
String returnedValue ="";
Configuration config = Configuration.defaultConfiguration().addOptions(Option.SUPPRESS_EXCEPTIONS);
try {
returnedValue = ""+JsonPath.using(config).parse(jsonContent).read(jsonPath);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return returnedValue;
}
return null;
任何人都知道为什么我有这个错误,我可以用另一个库或方法绕过它。
谢谢
您收到错误的原因非常正当,即不是有效的 jsonpath 查询。
如果您转到 https://jsonpath.herokuapp.com/ ( which uses jayway ) and enter the same data and path you will see this is not a valid jsonpath query for jayway, or two of the other implementations, the only one that does not fail outright does not return what you are expecting. I think you need to go back and re-read the jsonpath documentation,因为此语法显然无效。
正确的语法是 $.response.items[?(@.code=='500')].label
作为 documentation clearly states。
我不会依赖不会因语法错误而失败的实现。
假设我有这个 JSON:
{
"response" : {
"code" : "XXX",
"label" : "Lorem Ipsum",
"items" : [
{
"code" : "200",
"label" : "200 !!!"
},
{
"code" : "300",
"label" : "300 !!!!!"
},
{
"code" : "500",
"label" : "+500 !!!!!"
}]
}
}
我想在 Java 中 code
= 500(例如)时获取项目的标签。
我正在使用 jayWay
库和这个 jsonPath:
"$.response.items[?(@.code='500')].label"
我在解析时遇到此错误:Expected character: )
java代码:
public static String getElementValueByJsonPath(String jsonContent, String jsonPath) {
if (checkJsonValidity(jsonContent)) {
String returnedValue ="";
Configuration config = Configuration.defaultConfiguration().addOptions(Option.SUPPRESS_EXCEPTIONS);
try {
returnedValue = ""+JsonPath.using(config).parse(jsonContent).read(jsonPath);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return returnedValue;
}
return null;
任何人都知道为什么我有这个错误,我可以用另一个库或方法绕过它。
谢谢
您收到错误的原因非常正当,即不是有效的 jsonpath 查询。
如果您转到 https://jsonpath.herokuapp.com/ ( which uses jayway ) and enter the same data and path you will see this is not a valid jsonpath query for jayway, or two of the other implementations, the only one that does not fail outright does not return what you are expecting. I think you need to go back and re-read the jsonpath documentation,因为此语法显然无效。
正确的语法是 $.response.items[?(@.code=='500')].label
作为 documentation clearly states。
我不会依赖不会因语法错误而失败的实现。