24

我将 JSON 作为字符串,将 JSONPath 作为字符串。我想用 JSON 路径查询 JSON,得到结果 JSON 作为字符串。

我认为Jayway 的 json-path 是标准的. 然而,在线 API与您从 Maven 获得的实际库没有太大关系。不过, GrepCode 的版本大致匹配。

看来我应该能够做到:

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

问题是read它会根据 JSONPath 实际找到的内容(例如 a List<Object>Stringdouble等)返回它感觉最合适的任何内容,因此我的代码会为某些查询引发异常。假设有某种方法可以查询 JSON 并取回 JSON,这似乎是很合理的。有什么建议么?

4

4 回答 4

12

由于上述所有答案/评论,在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 <T>
 * @return result
 */
<T> T read(JsonPath path, Class<T> type);

希望这对任何人都有帮助。

于 2017-02-17T19:04:54.220 回答
11

肯定存在一种查询 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":{"district":"Ghaziabad","pin":201001,"pre_paid":"Y","cash":"Y","pickup":"Y","re​​pl":" N","cod":"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 区 :: Ghaziabad

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

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

于 2015-05-23T08:03:17.463 回答
8

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

截至 2018 年 9 月,以下是获取 Jackson JsonNode 结果的方法:

Configuration jacksonConfig = Configuration.builder()
                              .mappingProvider( new JacksonMappingProvider() )
                              .jsonProvider( new JacksonJsonProvider() )
                              .build();

JsonNode node = JsonPath.using( jacksonConfig ).parse(jsonString);
于 2018-09-16T02:25:20.870 回答
-1

查看 jpath API。它相当于 JSON 数据的 xpath。您可以通过提供将遍历 JSON 数据并返回请求值的 jpath 来读取数据。

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

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

自述文件 -

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

于 2017-03-29T17:11:00.683 回答