接口自动化之thirdDay JsonPathTest 定位

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NeilNiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值