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:

huangapple go评论82阅读模式
英文:

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("{\"structure\": {\"tables\": {\"category\": \"vehicle\"}}, \"data\": {}}");
    Object jsonPathArray = JsonPath.read(jObject, "$.structure.tables");

    System.out.println(jsonPathArray);
}

异常信息:

Exception in thread "main" com.jayway.jsonpath.PathNotFoundException: 预期在路径 $ 中找到具有属性 ['structure'] 的对象,但找到了 'org.json.JSONObject'。根据 JsonProvider,这不是一个 JSON 对象:'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)

如何解决上述异常?

英文:

I am using json-path com.jayway.jsonpath:2.4.0`

Java code:

 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:

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)

how to solve the above exception?

Thanks

答案1

得分: 9

你可以通过配置JsonPath来使用JsonOrgJsonProvider提供程序来实现此目标,因为默认情况下它使用JsonSmartJsonProvider,所以当你将JSONObject传递给该方法时,它无法遍历对象结构:

第一种方法:

public static void main(String[] args) {
    JSONObject jObject = new JSONObject("{\"structure\": {\"tables\": {\"category\": \"vehicle\"}}, \"data\": {}}");

    Configuration configuration = Configuration.builder()
            .jsonProvider(new JsonOrgJsonProvider())
            .build();

    JsonPath jsonPath = JsonPath.compile("$.structure.tables");
    Object jsonPathArray = jsonPath.read(jObject, configuration);

    System.out.println(jsonPathArray);
}

第二种方法:

public static void main(String[] args) {
    JSONObject jObject = new JSONObject("{\"structure\": {\"tables\": {\"category\": \"vehicle\"}}, \"data\": {}}");

    Object jsonPathArray = JsonPath.read(jObject.toString(), "$.structure.tables");

    System.out.println(jsonPathArray);
}

输出结果在两种情况下都是:

{category=vehicle}
英文:

You can achieve this by configuring JsonPath to use JsonOrgJsonProvider provider because by default it uses JsonSmartJsonProvider so when you pass JSONObject to this method it cannot traverse the object structure :

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);
}

or by passing a String directly :

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);
}

Output in both cases :

{category=vehicle}

答案2

得分: 1

只需使用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());
英文:

Simply used JacksonJsonProvider to solve this as Jackson is a well-developed library that understands the object structure and hierarchy.

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);
}

In case of Java Object as input in place of JSONObject,use an ObjectMapper to use the same function as above.

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());

huangapple
  • 本文由 发表于 2020年8月29日 15:51:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63644711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定