JSONPath在Java中的基本用法 [英] Basic use of JSONPath in Java

查看:457
本文介绍了JSONPath在Java中的基本用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have JSON as a string and a JSONPath as a string. I'd like to query the JSON with the JSON path, getting the resulting JSON as a string.

我收集 Jayway的json-path 是标准在线API但是,与你从Maven获得的实际图书馆 GrepCode的版本大致匹配。

I gather that Jayway's json-path is the standard. The online API, however, doesn't have have much relation to the actual library you get from Maven. GrepCode's version roughly matches up though.

看起来我应该能够做到:

It seems like I ought to be able to do:

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

问题是读取返回任何它根据JSONPath实际发现的内容感觉最合适(例如 List< Object> String double 等),因此我的代码会针对某些查询抛出异常。假设有一些方法可以查询JSON并获取JSON,这似乎是合理的。有什么建议吗?

The problem is that read returns whatever it feels most appropriate based on what the JSONPath actually finds (e.g. a List<Object>, String, double, etc.), thus my code throws an exception for certain queries. It seems pretty reasonable to assume that there'd be some way to query JSON and get JSON back; any suggestions?

推荐答案

肯定有一种方法可以查询Json并使用JsonPath获取Json。
参见下面的示例:

There definitely exists a way to query Json and get Json back using JsonPath. See example below:

 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。

The output of the above will be inner Json.


[{postal_code:{district:Ghaziabad,pin:201001,pre_paid:Y,cash:Y,pickup : Y, REPL: N, 鳕鱼: Y, is_oda: N, sort_code: GB, STATE_CODE: UP}}]

[{"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"}}]

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

Now each individual name/value pairs can be parsed by iterating the List (JsonNode) we got above.

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 :: Ghaziabad

pin :: 201001 district :: Ghaziabad

取决于你想要的Json解析,您可以决定是获取List还是只获取一个String / Long值。

Depending upon the Json you are trying to parse, you can decide whether to fetch a List or just a single String/Long value.

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

Hope it helps in solving your problem.

这篇关于JSONPath在Java中的基本用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆