JSONPath在Java中的基本用法

我将JSON作为字符串,将JSONPath作为字符串。 我想用JSON路径查询JSON,将生成的JSON作为字符串。

我认为Jayway的json路径 是标准 。 但是, 在线API与您从Maven获得的实际库没有多大关系。 GrepCode的版本大致匹配。

看起来我应该能做到:

String originalJson; //these are initialized to actual data String jsonPath; String queriedJson = JsonPath.read(originalJson, jsonPath); 

问题是read根据JSONPath实际找到的read返回任何最合适的内容(例如ListStringdouble等),因此我的代码会抛出某些查询的exception。 假设有一些方法可以查询JSON并获取JSON,这似乎是合理的。 有什么建议么?

肯定存在一种方法来查询Json并使用JsonPath返回Json。 见下面的例子:

  String jsonString = "{\"delivery_codes\": [{\"postal_code\": {\"district\": \"Ghaziabad\", \"pin\": 201001, \"pre_paid\": \"Y\", \"cash\": \"Y\", \"pickup\": \"Y\", \"repl\": \"N\", \"cod\": \"Y\", \"is_oda\": \"N\", \"sort_code\": \"GB\", \"state_code\": \"UP\"}}]}"; String jsonExp = "$.delivery_codes"; JsonNode pincodes = JsonPath.read(jsonExp, jsonString, JsonNode.class); System.out.println("pincodesJson : "+pincodes); 

上面的输出将是内部Json。

[{ “POSTAL_CODE”:{ “区”: “加济阿巴德”, “针”:201001, “pre_paid”: “Y”, “现金”: “Y”, “皮卡”: “Y”, “REPL”:” N”, “鳕鱼”: “Y”, “is_oda”: “N”, “sort_code”: “GB”, “STATE_CODE”: “UP”}}]

现在可以通过迭代我们上面得到的List(JsonNode)来解析每个单独的名称/值对。

 for(int i = 0; i< pincodes.size();i++){ JsonNode node = pincodes.get(i); String pin = JsonPath.read("$.postal_code.pin", node, String.class); String district = JsonPath.read("$.postal_code.district", node, String.class); System.out.println("pin :: " + pin + " district :: " + district ); } 

输出将是:

pin :: 201001 district ::加济阿巴德

根据您尝试解析的Json,您可以决定是获取List还是仅获取单个String / Long值。

希望它有助于解决您的问题。

自上述所有答案/评论以来,在jayway JsonPath上发现的Java JsonPath API可能已经发生了一些变化。 文档也是。 只需按照上面的链接阅读README.md ,它包含一些非常明确的使用文档IMO。

基本上,从目前最新的2.2.0版库开始,有几种不同的方法可以实现此处的要求,例如:

 Pattern: -------- String json = "{...your JSON here...}"; String jsonPathExpression = "$...your jsonPath expression here..."; J requestedClass = JsonPath.parse(json).read(jsonPathExpression, YouRequestedClass.class); Example: -------- // For better readability: {"store": { "books": [ {"author": "Stephen King", "title": "IT"}, {"author": "Agatha Christie", "title": "The ABC Murders"} ] } } String json = "{\"store\": { \"books\": [ {\"author\": \"Stephen King\", \"title\": \"IT\"}, {\"author\": \"Agatha Christie\", \"title\": \"The ABC Murders\"} ] } }"; String jsonPathExpression = "$.store.books[?(@.title=='IT')]"; JsonNode jsonNode = JsonPath.parse(json).read(jsonPathExpression, JsonNode.class); 

作为参考,调用’JsonPath.parse(..)’将返回类’ JsonContent ‘的对象,实现一些接口,如’ ReadContext ‘,它包含几个不同的’read(..)’操作,例如演示的一个以上:

 /** * Reads the given path from this context * * @param path path to apply * @param type expected return type (will try to map) * @param  * @return result */  T read(JsonPath path, Class type); 

希望这有助于任何人。

对于那些想知道为什么这些年代的答案不起作用的人,你可以从测试用例中学到很多东西。

截至2018年9月,您可以通过以下方式获得Jackson JsonNode的结果:

 Configuration jacksonConfig = Configuration.builder() .mappingProvider( new JacksonMappingProvider() ) .jsonProvider( new JacksonJsonProvider() ) .build(); JsonNode node = JsonPath.using( jacksonConfig ).parse(jsonString); 

查看jpath API。 它是JSON Data的xpath等价物。 您可以通过提供将遍历JSON数据并返回所请求值的jpath来读取数据。

这个Java类是实现,它有关于如何调用API的示例代码。

https://github.com/satyapaul/jpath/blob/master/JSONDataReader.java

自述 –

https://github.com/satyapaul/jpath/blob/master/README.md