JSON取数工具——JsonPath

Posted 在路上的德尔菲

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSON取数工具——JsonPath相关的知识,希望对你有一定的参考价值。

简介

JsonPath是一种简单的方法来提取给定JSON文档的部分内容。

Data may be interactively found and extracted out of JSON structures on the client without special scripting.
JSON data requested by the client can be reduced to the relevant parts on the server, such minimizing the bandwidth usage of the server response.

二、引入

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.4.0</version>
</dependency>

三、使用规则介绍

{
    "tool": 
    {
        "jsonpath": 
        {
            "creator": 
            {
                "name": "Jayway Inc.",
                "location": 
                [
                    "Malmo",
                    "San Francisco",
                    "Helsingborg"
                ]
            }
        }
    },
 
    "book": 
    [
        {
            "title": "Beginning JSON",
            "price": 49.99
        },
         {
            "title": "JSON at Work",
            "price": 29.99
        }
    ]
}

3.1Notation

具有两种表示风格

$.tool.jsonpath.creator.location[2]
------------建议使用👆-----------------
$['tool']['jsonpath']['creator']['location'][2]

3.2 Operators 操作符

Root node ($) :根节点,无论是Object还是Array

Current node (@) :当前正在处理的节点, $…book[?(@.price == 22.99)]

Wildcard () :特定范围内所有的元素,$…book.[].title

3.3 Functions

  • min() 输出数组中的元素的最小值

  • max() 输出数组中元素的最大值

  • avg() 输出数组中所有元素的平均值

  • stddev() 输出数组的标准差

  • length() 输出数组的元素个数

3.4 Filter

Filter用于过滤数组,典型的用法如 [?(@.age > 18)] 当前节点age属性大于18,复杂的过滤器包括与逻辑&&和或逻辑||,如 [?(@.color == ‘blue’)] or [?(@.color == “blue”)] 其中String类型需要用单引号或双引

常用的过滤有 == 判断是否相同, !=判断是否不相同, >=大于等于, <=小于等于, >大于 , < 小于 ,in 包括在内 ,nin不包括在内

3.5 使用例子

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
                       {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}
String json = "..."; //假设上面json数据

List<String> authors = JsonPath.read(json, "$.store.book[*].author"); //返回所有书的作者
List<String> authors = JsonPath.read(json, "$..author"); //同样返回所有书的作者

--------------返回结果👇----------------
[
   "Nigel Rees",
   "Evelyn Waugh",
   "Herman Melville",
   "J. R. R. Tolkien"
]

3.6 注意事项

//Will throw an java.lang.ClassCastException    
List<String> list = JsonPath.parse(json).read("$.store.book[0].author")

//Works fine
String author = JsonPath.parse(json).read("$.store.book[0].author")

//denote class type , Works fine
String json = "{\\"date_as_long\\" : 1411455611975}";
Date date = JsonPath.parse(json).read("$['date_as_long']", Date.class);

附:
Github: https://github.com/json-path/JsonPath

语句查询: http://jsonpath.herokuapp.com/

以上是关于JSON取数工具——JsonPath的主要内容,如果未能解决你的问题,请参考以下文章

带有JSONPath的JSON.NET SelectToken

从 jsonpath 和 xpath 到 SPL

从 jsonpath 和 xpath 到 SPL

从 jsonpath 和 xpath 到 SPL

从 jsonpath 和 xpath 到 SPL

超好用的JSON解析工具—JSONPath