Java中JSONPath的基本用法
我将JSON作为字符串,将JSONPath作为字符串。我想用JSON路径查询JSON,将得到的JSON作为字符串 我想是这样。然而,与的关系不大。不过大致上是吻合的 看来我应该能够做到:Java中JSONPath的基本用法,java,json,jsonpath,Java,Json,Jsonpath,我将JSON作为字符串,将JSONPath作为字符串。我想用JSON路径查询JSON,将得到的JSON作为字符串 我想是这样。然而,与的关系不大。不过大致上是吻合的 看来我应该能够做到: String originalJson; //these are initialized to actual data String jsonPath; String queriedJson = JsonPath.<String>read(originalJson, jsonPath); stri
String originalJson; //these are initialized to actual data
String jsonPath;
String queriedJson = JsonPath.<String>read(originalJson, jsonPath);
stringoriginaljson//这些被初始化为实际数据
字符串jsonPath;
字符串queryedjson=JsonPath.read(originalJson,JsonPath);
问题是,
read
根据JSONPath实际找到的内容返回它认为最合适的内容(例如,列表
,字符串
,双精度
,等等),因此我的代码对某些查询抛出异常。假设有某种方法可以查询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
[{“邮政编码”:{“地区”:“Ghaziabad”,“pin”:201001,“预付款”:“Y”,“现金”:“Y”,“皮卡”:“Y”,“回复”:“N”,“cod”:“Y”,“is_oda”:“N”,“分类编码”:“GB”,“州编码”:“UP”}]
现在可以通过迭代上面的列表(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 );
}
for(int i=0;i
输出将是:
pin::201001地区::Ghaziabad
根据您试图解析的Json,您可以决定是获取列表还是仅获取单个字符串/长值
希望它有助于解决您的问题。在找到的Java JsonPath API可能在上述所有答案/注释之后发生了一些变化。文件也是。只需按照上面的链接阅读,它包含了一些非常清晰的使用说明文档 基本上,从库的最新版本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(..”)将返回类“”的对象,实现一些接口,如“”,其中包含几个不同的“读取(..)”操作,如上面演示的操作:
/**
* 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);
/**
*从该上下文读取给定路径
*
*@param-path要应用的路径
*@param type预期返回类型(将尝试映射)
*@param
*@返回结果
*/
T read(JsonPath路径,类类型);
希望这对任何人都有帮助。查看jpath API。它相当于JSON数据的xpath。您可以通过提供jpath来读取数据,jpath将遍历JSON数据并返回请求的值 这个Java类是实现,它有关于如何调用API的示例代码 自述-
对于那些想知道为什么这些老掉牙的答案不管用的人,你可以从中学到很多 截至2018年9月,以下是获得Jackson JsonNode结果的方法:
Configuration jacksonConfig = Configuration.builder()
.mappingProvider( new JacksonMappingProvider() )
.jsonProvider( new JacksonJsonProvider() )
.build();
JsonNode node = JsonPath.using( jacksonConfig ).parse(jsonString);
我不知道JSONPath,但我怀疑你链接的“在线API”是Jayway库的官方API。既然它看起来像JavaDoc,为什么不下载最新版本并直接阅读当前的JavaDoc呢?我有,它与GrepCode版本大致匹配。(我试图让人们在不下载库的情况下轻松查看API)总体而言,库的文档非常糟糕,但我尝试做的似乎非常基本,我认为这是可能的。从当前的GrepCode版本来看,应用的过滤器似乎解析JSON并返回任何适当的内容。如果您想要返回JSON,也许您可以提供自己的过滤器,以某种方式使用配置,或者可能只是使用JSONPath配置使用的同一个JsonProvider将结果转换回JSON,即类似于
String JSON=provider.toJson(JSONPath.read(…)
您使用的是哪个版本?在2.2.0版中,我没有看到任何read()方法接受JsonNode参数。我也在尝试相同的方法,但未找到此方法也找不到。您找到版本了吗?欢迎链接到解决方案,但请确保您的答案在没有它的情况下是有用的:这样您的其他用户就会知道它是什么以及为什么存在,然后引用您链接到的页面的最相关部分,以防目标页面不可用。添加了其他信息。我认为代码非常直截了当,不言自明。此外,自述文件有解释,也没有几个例子。我在避免复制那些内容。无论如何,谢谢!这里的JsonPath.parse().read()语法适用于我,而上面答案中的代码失败了。谢谢你的发帖!