1. 程式人生 > 實用技巧 >【java基礎操作】

【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"]