• 【java基础操作】


    一、基础操作

    1.1 如何使用JsonPath筛选json数据?

    准备Json数据

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

    常用操作

    JsonPath能做什么?
    类似xpath的作用,可以快速定位插入数据删除数据及查询数据
    read方法和eval的区别?
    read方法会调用eval方法,read方法的参数为string eval的为object使用eval的时候尽量使用jsonObject

     List<String> authors = (List<String>)JSONPath.read(json, "$..book.author");
     System.out.println(authors);  // ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
     Integer authors = (Integer)JSONPath.read(json, "$..book.length()");
     System.out.println(authors); // 4
     String author = (String)JSONPath.read(json, "$.store.book[0].author");  
     System.out.println(author)  //  Nigel Rees
     List<String> authors = (List<String>)JSONPath.read(json, "$.store.book[0,1].author");
     System.out.println(authors); // ["Nigel Rees","Evelyn Waugh"]
     List<String> authors = (List<String>)JSONPath.read(json, "$.store.book[1:].author");
     System.out.println(authors); // ["Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
     List<String> authors = (List<String>)JSONPath.read(json, "$.store.book[-1:].author");
     System.out.println(authors); // ["J. R. R. Tolkien"]
     List<String> authors = (List<String>)JSONPath.read(json, "$.store.book[(@.length-1)].author");
     System.out.println(authors); // ["J. R. R. Tolkien"]
     List<String> authors = (List<String>)JSONPath.read(json, "$.store.book[?(@.isbn)].author");
     System.out.println(authors); //["Herman Melville","J. R. R. Tolkien"]
     List<JSONObject> authors = (List<JSONObject>)JSONPath.read(json, "$.store.book[?(@.category=='reference')]");
     System.out.println(authors); // [{"author":"Nigel Rees","price":8.95,"category":"reference","title":"Sayings of the Century"}]
     List<String> authors = (List<String>)JSONPath.read(json, "$..book[?(@.price<10)].author");
     System.out.println(authors); // ["Nigel Rees","Herman Melville"]
     List<String> authors = (List<String>)JSONPath.read(json, "$..book[?(@.price<10 || @.author == 'Evelyn Waugh')].author");
     System.out.println(authors); // ["Nigel Rees","Evelyn Waugh","Herman Melville"]
     List<String> authors = (List<String>)JSONPath.read(json, "$..book[?(@.price<10 && @.author == 'Evelyn Waugh')].author");
     System.out.println(authors); // []
    
     List<String> authors = (List<String>)JSONPath.read(json, "$..book[price > 10 || category = 'reference'].author");
     System.out.println(authors);  //["Nigel Rees","Evelyn Waugh","J. R. R. Tolkien"]
     List<String> authors = (List<String>)JSONPath.read(json, "$..book[price > 10 && title = 'The Lord of the Rings'].author");
     System.out.println(authors);  //["J. R. R. Tolkien"]
     List<String> authors = (List<String>)JSONPath.read(json, "$..book[author =~ /.*REES/i].author");
     System.out.println(authors)  // ["Nigel Rees"]
     List<String> authors = (List<String>)JSONPath.read(json, "$..book[category in ('reference')].author");
     System.out.println(authors); // ["Nigel Rees"]
     List<String> authors = (List<String>)JSONPath.read(json, "$..book[category not in ('reference')].author");
     System.out.println(authors); // ["Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
     List<String> authors = (List<String>)JSONPath.read(json, "$..book[author like '%Waugh%'].author");
     System.out.println(authors); //["Evelyn Waugh"]
     List<String> authors = (List<String>)JSONPath.read(json, "$..book[?(isbn)].author");
     System.out.println(authors); //["Herman Melville","J. R. R. Tolkien"]
    
  • 相关阅读:
    【秋招必备】Redis面试题(2021最新版)
    【秋招必备】Spring Boot面试题(2021最新版)
    【秋招必备】Java基础知识面试题(2021最新版)
    用友二面:如何设计一个高可用、高并发秒杀系统
    万字长文,带你深入理解Java虚拟机!
    小米面试官:说说Spring源码里面的Bean的生命周期!
    苏宁易购三面:写一个脚本获取Linux系统CPU的详细信息,并说出原理!
    易错点。
    APP间传递消息
    KVC, KVO
  • 原文地址:https://www.cnblogs.com/mankeu/p/14183498.html
Copyright © 2020-2023  润新知