预期在路径 $ 中找到 属性 ['xyz'] 的对象,但找到了 'org.json.JSONObject'。根据 JsonProvider,这不是 json 对象:
Expected to find an object with property ['xyz'] in path $ but found 'org.json.JSONObject'. This is not a json object according to the JsonProvider:
我正在使用 json-path
com.jayway.jsonpath:2.4.0`
Java代码:
public static void main( String[] args )
{
JSONObject jObject =new JSONObject("{\r\n \"structure\": {\r\n \"tables\": {\r\n \"category\": \"vehicle\"\r\n }\r\n },\r\n \"data\": {}\r\n}") ;
Object jsonPathArray = JsonPath.read(jObject,"$.structure.tables");
System.out.println(jsonPathArray);
}
异常:
Exception in thread "main" com.jayway.jsonpath.PathNotFoundException: Expected to find an object with property ['structure'] in path $ but found 'org.json.JSONObject'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:71)
at com.jayway.jsonpath.internal.path.RootPathToken.evaluate(RootPathToken.java:62)
at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:53)
at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:61)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:187)
at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:102)
at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:89)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:488)
at rxjava.testapp.App.main(App.java:21)
如何解决上述异常?
谢谢
您可以通过将 JsonPath
配置为使用 JsonOrgJsonProvider
提供程序来实现此目的,因为默认情况下它使用 JsonSmartJsonProvider
因此当您将 JSONObject
传递给此方法时它无法遍历对象结构:
public static void main( String[] args ) {
JSONObject jObject = new JSONObject("{\r\n \"structure\": {\r\n \"tables\": {\r\n \"category\": \"vehicle\"\r\n }\r\n },\r\n \"data\": {}\r\n}") ;
Configuration configuration = Configuration.builder()
.jsonProvider(new JsonOrgJsonProvider())
.build();
JsonPath jsonPath = JsonPath.compile("$.structure.tables");
Object jsonPathArray= jsonPath.read(jObject, configuration);
System.out.println(jsonPathArray);
}
或直接传递 String
:
public static void main( String[] args ) {
JSONObject jObject = new JSONObject("{\r\n \"structure\": {\r\n \"tables\": {\r\n \"category\": \"vehicle\"\r\n }\r\n },\r\n \"data\": {}\r\n}") ;
Object jsonPathArray= JsonPath.read(jObject.toString(),"$.structure.tables");
System.out.println(jsonPathArray);
}
两种情况下的输出:
{category=vehicle}
简单地使用 JacksonJsonProvider
来解决这个问题,因为 Jackson 是一个理解对象结构和层次结构的完善的库。
public static void main( String[] args ) {
JSONObject jObject = new JSONObject("{\r\n \"structure\": {\r\n \"tables\": {\r\n \"category\": \"vehicle\"\r\n }\r\n },\r\n \"data\": {}\r\n}") ;
Configuration configuration = Configuration.builder()
.jsonProvider(new JacksonJsonProvider())
.build();
DocumentContext jsonContext = JsonPath.using(conf).parse(jObject.toString());
Object jsonPathArray= jsonContext.read("$.structure.tables");
System.out.println(jsonPathArray);
}
如果输入 Java Object
代替 JSONObject
,请使用 ObjectMapper
以使用与上述相同的功能。
ObjectMapper mapper = new ObjectMapper();
String jsonData = mapper.writeValueAsString(object);
Configuration conf = Configuration.builder()
.jsonProvider(new JacksonJsonProvider())
.build();
DocumentContext jsonContext = JsonPath.using(conf).parse(jObject.toString());
我正在使用 json-path
com.jayway.jsonpath:2.4.0`
Java代码:
public static void main( String[] args )
{
JSONObject jObject =new JSONObject("{\r\n \"structure\": {\r\n \"tables\": {\r\n \"category\": \"vehicle\"\r\n }\r\n },\r\n \"data\": {}\r\n}") ;
Object jsonPathArray = JsonPath.read(jObject,"$.structure.tables");
System.out.println(jsonPathArray);
}
异常:
Exception in thread "main" com.jayway.jsonpath.PathNotFoundException: Expected to find an object with property ['structure'] in path $ but found 'org.json.JSONObject'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:71)
at com.jayway.jsonpath.internal.path.RootPathToken.evaluate(RootPathToken.java:62)
at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:53)
at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:61)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:187)
at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:102)
at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:89)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:488)
at rxjava.testapp.App.main(App.java:21)
如何解决上述异常?
谢谢
您可以通过将 JsonPath
配置为使用 JsonOrgJsonProvider
提供程序来实现此目的,因为默认情况下它使用 JsonSmartJsonProvider
因此当您将 JSONObject
传递给此方法时它无法遍历对象结构:
public static void main( String[] args ) {
JSONObject jObject = new JSONObject("{\r\n \"structure\": {\r\n \"tables\": {\r\n \"category\": \"vehicle\"\r\n }\r\n },\r\n \"data\": {}\r\n}") ;
Configuration configuration = Configuration.builder()
.jsonProvider(new JsonOrgJsonProvider())
.build();
JsonPath jsonPath = JsonPath.compile("$.structure.tables");
Object jsonPathArray= jsonPath.read(jObject, configuration);
System.out.println(jsonPathArray);
}
或直接传递 String
:
public static void main( String[] args ) {
JSONObject jObject = new JSONObject("{\r\n \"structure\": {\r\n \"tables\": {\r\n \"category\": \"vehicle\"\r\n }\r\n },\r\n \"data\": {}\r\n}") ;
Object jsonPathArray= JsonPath.read(jObject.toString(),"$.structure.tables");
System.out.println(jsonPathArray);
}
两种情况下的输出:
{category=vehicle}
简单地使用 JacksonJsonProvider
来解决这个问题,因为 Jackson 是一个理解对象结构和层次结构的完善的库。
public static void main( String[] args ) {
JSONObject jObject = new JSONObject("{\r\n \"structure\": {\r\n \"tables\": {\r\n \"category\": \"vehicle\"\r\n }\r\n },\r\n \"data\": {}\r\n}") ;
Configuration configuration = Configuration.builder()
.jsonProvider(new JacksonJsonProvider())
.build();
DocumentContext jsonContext = JsonPath.using(conf).parse(jObject.toString());
Object jsonPathArray= jsonContext.read("$.structure.tables");
System.out.println(jsonPathArray);
}
如果输入 Java Object
代替 JSONObject
,请使用 ObjectMapper
以使用与上述相同的功能。
ObjectMapper mapper = new ObjectMapper();
String jsonData = mapper.writeValueAsString(object);
Configuration conf = Configuration.builder()
.jsonProvider(new JacksonJsonProvider())
.build();
DocumentContext jsonContext = JsonPath.using(conf).parse(jObject.toString());