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