package com.thirdDay;
import com.alibaba.fastjson.JSON;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.JsonPath;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import static com.jayway.jsonpath.Criteria.where;
import static com.jayway.jsonpath.Filter.filter;
/**
* Created by Administrator on 2017/8/20 0020.
*/
public class JsonPathTest {
/*
* @Description:获取book数组下所有的author的值
* */
@Test
public void getAllAttribute() {
String json = getJson();
// * 代表所有
// 跟路径bookstore 对象下面的 book对象(list) 下面所有对象的 author属性的值
//最根本的语法 $.bookstore.book[*].author
//{ "category": "reference",\"author\": \"Nigel Rees\",\n" \"title\": \"Sayings of the Century\",\n" +\"price\": 8.95\n" +" }
List<String> authors = JsonPath.read(json, "$.bookstore.book[*].author");
for (String s : authors) {
System.out.println(s);
}
}
/*
* @Description:获获取book第一个2对象的author的值
* */
@Test
public void getFirstAttribute() {
String json = getJson();
String author = JsonPath.read(json, "$.bookstore.book[1].author");
System.out.println(author);
}
/*
* @Description:获获取book属性中category=reference的对象
* */
@Test
public void getObjectByAttribute() {
String json = getJson();
List<Object> books = JsonPath.read(json, "$.bookstore.book[?(@.category == 'reference')]");
json = JSON.toJSONString(books);
System.out.println(json);
}
/*
* @Description:获获取book属性中category=reference的对象filter表达式
* */
@Test
public void getObjectOfAttributeByFilter() {
String json = getJson();
//filter 过滤器
List<Object> books = JsonPath.read(json, "$.bookstore.book[?]", filter(where("category").is("reference")));
json = JSON.toJSONString(books);
System.out.println(json);
}
/*
* @Description:获获取book属性中price大于10的对象
* */
@Test
public void getObjectByAttributeOfPrice() {
String json = getJson();
List<Object> books = JsonPath.read(json, "$.bookstore.book[?(@.price > 10)]");
json = JSON.toJSONString(books);
System.out.println(json);
}
/*
* @Description:获获取book属性中price大于10的对象
* */
@Test
public void getObjectByAttributeOfGt() {
String json = getJson();
//gt代表大于10, lt代表小于10
List<Object> books = JsonPath.read(json, "$.bookstore.book[?]", filter(where("price").gt(10)));
json = JSON.toJSONString(books);
System.out.println(json);
}
/*
* @Description:获获取book属性中含有isbn属性的对象
* */
@Test
public void getObjectExistIsbn() {
String json = getJson();
List<Object> books = JsonPath.read(json, "$.bookstore.book[?(@.isbn)]");
json = JSON.toJSONString(books);
System.out.println(json);
}
/*
* @Description:获获取book属性中含有isbn属性的对象,用filter表达式
* */
@Test
public void getObjectExistIsbnByFilter() {
String json = getJson();
List<Object> books = JsonPath.read(json, "$.bookstore.book[?]", filter(where("isbn").exists(true)));
json = JSON.toJSONString(books);
System.out.println(json);
}
/*
* @Description:链式过滤器,多个条件
* */
@Test
public void chainedFilters() {
String json = getJson();
Filter filter = filter(where("isbn").exists(true).and("category").in("fiction", "reference"));
List<Object> books = JsonPath.read(json, "$.bookstore.book[?]", filter);
json = JSON.toJSONString(books);
System.out.println(json);
}
/*
* @Description:自定义过滤器
* */
@Test
public void customFilters() {
String json = getJson();
Filter myFilter = new Filter.FilterAdapter<Map<String, Object>>() {
@Override
public boolean accept(Map<String, Object> map) {
return map.containsKey("isbn");
}
};
List<Object> books = JsonPath.read(json, "$.bookstore.book[?]", myFilter);
json = JSON.toJSONString(books);
System.out.println(json);
}
/*
* @Description:编译路径,你可以预先编译一个路径,并使用它多次
* */
@Test
public void compiledPath(){
String json = getJson();
JsonPath path = JsonPath.compile("$.bookstore.book[*]");
List<Object> books = path.read(json);
json = JSON.toJSONString(books);
System.out.println(json);
}
private String getJson() {
return "{ \"bookstore\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99,\n" +
" \"isbn\": \"0-553-21311-3\"\n" +
" },{\"author\":\"jiaou\"," +
"\"price\":18" +
"}\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95\n" +
" }\n" +
" }\n" +
"}";
}
public String getMyJson() {
return "{\n" +
" \"code\": 200,\n" +
" \"data\": {\n" +
" \"isExist\": false,\n" +
" \"dataList\": [\n" +
" {\n" +
" \"currency\": \"USD\",\n" +
" \"iPrice\": 12\n" +
" },\n" +
" {\n" +
" \"currency\": \"EUR\",\n" +
" \"iPrice\": 34\n" +
" },\n" +
" {\n" +
" \"currency\": \"CNY\",\n" +
" \"iPrice\": 56\n" +
" }\n" +
" ],\n" +
" \"time\": 1453193714993,\n" +
" \"pid\": 78630\n" +
" },\n" +
" \"success\": true\n" +
"}\n";
}
}