zoukankan      html  css  js  c++  java
  • JsonPath实践(二)

    书接上文:JsonPath实践(一)

    本期聊一下如何使用JSonpath标记语法处理,json对象中的数组主要内容是提取数组中对象和对象集合。

    json数据

    首先看官方给的json数据的Demo(我做了一点点修改):

    JSONObject json = JSON.parseObject("{" +
                    "    "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," +
                    "    "ss": [32,32,4,23]" +
                    "}");
    

    获取数组中的有序对象

    jsonpath$.store.book[2]

    • 这里注意索引是从0开始的。

    jsonpath$.store.book[-1]

    • 这里表示倒数第一个对象

    代码:

            Object read = JsonPath.read(json, "$.store.book[2]");
            output(JSON.parseObject(read.toString()));
    

    等效写法:

            JSONObject read = JsonPath.read(json, "$.store.book[2]");
            output(read);
    
    

    控制台输出:

    INFO-> 当前用户:fv,IP:10.60.131.54,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
    INFO-> 
    ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
    >  {
    >  ① . "author":"Herman Melville",
    >  ① . "price":8.99,
    >  ① . "isbn":"0-553-21311-3",
    >  ① . "category":"fiction",
    >  ① . "title":"Moby Dick"
    >  }
    ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
    
    Process finished with exit code 0
    
    

    获取数组中的有序对象切片

    • 这里的语法类似于Python中对于数组的处理。

    jsonpath$.store.book[:2]

    • 倒数截取

    jsonpath$.store.book[-2:]

    代码:

            Object read = JsonPath.read(json, "$.store.book[2]");
            output(JSON.parseObject(read.toString()));
    

    等效写法省略……

    控制台输出:

    INFO-> 当前用户:fv,IP:10.60.192.21,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
    INFO-> 
    ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
    >  {
    >  ① . "author":"Nigel Rees",
    >  ① . "price":8.95,
    >  ① . "category":"reference",
    >  ① . "title":"Sayings of the Century"
    >  }
    ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
    INFO-> 
    ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
    >  {
    >  ① . "author":"Evelyn Waugh",
    >  ① . "price":12.99,
    >  ① . "category":"fiction",
    >  ① . "title":"Sword of Honour"
    >  }
    ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
    
    Process finished with exit code 0
    
    
    • 倒数截取
    
    INFO-> 当前用户:fv,IP:10.60.192.21,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
    INFO-> 
    ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
    >  {
    >  ① . "author":"Herman Melville",
    >  ① . "price":8.99,
    >  ① . "isbn":"0-553-21311-3",
    >  ① . "category":"fiction",
    >  ① . "title":"Moby Dick"
    >  }
    ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
    INFO-> 
    ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
    >  {
    >  ① . "author":"J. R. R. Tolkien",
    >  ① . "price":22.99,
    >  ① . "isbn":"0-395-19395-8",
    >  ① . "category":"fiction",
    >  ① . "title":"The Lord of the Rings"
    >  }
    ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
    
    Process finished with exit code 0
    
    

    • 公众号FunTester首发,更多原创文章:FunTester440+原创文章,欢迎关注、交流,禁止第三方擅自转载。

    热文精选

  • 相关阅读:
    Power of Cryptography
    Radar Installation
    Emag eht htiw Em Pleh
    Help Me with the Game
    89. Gray Code
    87. Scramble String
    86. Partition List
    85. Maximal Rectangle
    84. Largest Rectangle in Histogram
    82. Remove Duplicates from Sorted List II
  • 原文地址:https://www.cnblogs.com/FunTester/p/13507748.html
Copyright © 2011-2022 走看看