/** * Get the result of an Object path expression as a list. * * @param path The Object path. * @param <T> The list type * @return The object matching the Object path. A {@link java.lang.ClassCastException} will be thrown if the object * cannot be casted to the expected type. */ public <T> List<T> getList(String path) { return get(path); }
/** * Create a reciprocal applink without any auth enabled. * * @param request request to create the applink * @return created applink */ @Nonnull public TestApplink create(@Nonnull BackdoorCreateApplinkRequest request) { Response response = applinksTester.post(request); JsonPath result = response.getBody().jsonPath(); if (result.get("errors") != null) { throw new RuntimeException("Error response " + response.as(RestErrors.class)); } return new DefaultTestApplink.Builder() .from(result.getString("linkId"), instance) .to(result.getString("localId"), request.to) .build(); }
/** * Get the result of an Object path expression as a map. * * @param path The Object path. * @param <K> The type of the expected key * @param <V> The type of the expected value * @return The map matching the Object path. A {@link java.lang.ClassCastException} will be thrown if the object * cannot be casted to the expected type. */ public <K, V> Map<K, V> getMap(String path) { return get(path); }
/** * Get a Object graph with no named root element as a Java object. This is just a short-cut for * <p/> * <pre> * get(""); * </pre> * or * <pre> * get("$"); * </pre> * * @return The object matching the Object graph. This may be any primitive type, a List or a Map. A {@link java.lang.ClassCastException} will be thrown if the object * cannot be casted to the expected type. */ public <T> T get() { return get(""); }
Response res = given(). param("thingAccountIsfor","ABCD"). when(). get("http://machine:2343/rest/accounts/getaccount"); JsonPath jp = new JsonPath(res.asString()); String account = jp.get("account").toString();
JsonPath jPath = new JsonPath(jPathStrB.toString()); jPath.prettyPrint(); Float price = jPath.get("store.book.price.max()"); System.out.println("Max price: " + price); }
/** * Get the result of an Object path expression as a string. * * @param path The Object path. * @return The object matching the Object path. A {@link java.lang.ClassCastException} will be thrown if the object * cannot be casted to the expected type. */ public String getString(String path) { return ObjectConverter.convertObjectTo(get(path), String.class); }
/** * Get the result of an Object path expression as a boolean * * @param path The Object path. * @return The object matching the Object path. A {@link java.lang.ClassCastException} will be thrown if the object * cannot be casted to the expected type. */ public boolean getBoolean(String path) { return ObjectConverter.convertObjectTo(get(path), Boolean.class); }
/** * Get the result of an Object path expression as a char. * * @param path The Object path. * @return The object matching the Object path. A {@link java.lang.ClassCastException} will be thrown if the object * cannot be casted to the expected type. */ public char getChar(String path) { return ObjectConverter.convertObjectTo(get(path), Character.class); }
/** * Get the result of an Object path expression as a UUID. * * @param path The Object path. * @return The object matching the Object path. A {@link java.lang.ClassCastException} will be thrown if the object * cannot be casted to the expected type. */ public UUID getUUID(String path) { return ObjectConverter.convertObjectTo(get(path), UUID.class); }
public static void main(final String[] args) { JsonPath jsonPath = new JsonPath(yourJson).using(new JsonPathConfig("UTF-8")); System.out.println( jsonPath.get("years['2017'].weeks*.find {it.isoWeekNum == '1'}") ); }
/** * Get the result of an Object path expression as a double. * * @param path The Object path. * @return The object matching the Object path. A {@link java.lang.ClassCastException} will be thrown if the object * cannot be casted to the expected type. */ public double getDouble(String path) { final Object value = get(path); if (value instanceof Double) { return (Double) value; } return ObjectConverter.convertObjectTo(value, Double.class); }
/** * Get the result of an Object path expression as a float. * * @param path The Object path. * @return The object matching the Object path. A {@link java.lang.ClassCastException} will be thrown if the object * cannot be casted to the expected type. */ public float getFloat(String path) { final Object value = get(path); //Groovy will always return a Double for floating point values. if (value instanceof Double) { return ((Double) value).floatValue(); } else { return ObjectConverter.convertObjectTo(value, Float.class); } }
public static String[] getCoordinates(String address, String county) throws IOException, ParserConfigurationException, SAXException { address = address.replace(",", "+"); address = address.replace(" ", "+"); county = county.replace(" ", ""); String fullAddress = address+"+"+county; System.out.println(fullAddress); URL url = new URL("http://maps.google.com/maps/api/geocode/json?address="+fullAddress+"&sensor=false"); Response res = given(). when(). get(url); JsonPath jp = new JsonPath(res.asString()); String lat = jp.get("results.geometry.location.lat").toString(); String lng = jp.get("results.geometry.location.lng").toString(); String[] location = new String[2]; location[0] = lat; location[1] = lng; return location; }
/** * Get the result of an Object path expression as an int. * * @param path The Object path. * @return The int matching the Object path. A {@link java.lang.ClassCastException} will be thrown if the object * cannot be casted to the expected type. */ public int getInt(String path) { //The type returned from Groovy depends on the input, so we need to handle different numerical types. Object value = get(path); if (value instanceof Integer) { return (Integer) value; } else if (value instanceof Short) { return ((Short) value).intValue(); } else if (value instanceof Long) { return ((Long) value).intValue(); } else { return ObjectConverter.convertObjectTo(value, Integer.class); } }
/** * Get the result of an Object path expression as a list. * * @param path The Object path. * @param genericType The generic list type * @param <T> The type * @return The object matching the Object path. A {@link java.lang.ClassCastException} will be thrown if the object * cannot be casted to the expected type. */ public <T> List<T> getList(String path, Class<T> genericType) { final List<T> original = get(path); final List<T> newList = new LinkedList<T>(); if (original != null) { for (T t : original) { newList.add(ObjectConverter.convertObjectTo(t, genericType)); } } return Collections.unmodifiableList(newList); }
Response response = given() .given().header("Content-Language", "en_US") .contentType("application/json") .body(ApiBody) .when() .post(baseApiUrl); JsonPath jp = new JsonPath(response.asString()); log.info(response.asString()); String value = jp.get("valueToCheck").toString(); Assert.assertEquals(valueToCheckFromResponseJson, actualValue, "Value from API doesn't match Value from DB");
/** * Get the result of an Object path expression as a byte. * * @param path The Object path. * @return The object matching the Object path. A {@link java.lang.ClassCastException} will be thrown if the object * cannot be casted to the expected type. */ public byte getByte(String path) { //The type returned from Groovy depends on the input, so we need to handle different numerical types. Object value = get(path); if (value instanceof Byte) { return (Byte) value; } else if (value instanceof Long) { return ((Long) value).byteValue(); } else if (value instanceof Integer) { return ((Integer) value).byteValue(); } else { return ObjectConverter.convertObjectTo(value, Byte.class); } }
/** * 获取本次请求的响应信息 * * @param response */ private String getResponseInfo(Response response) { // TODO - 此处容易抛异常 if (response.contentType().contains("json")) { return "[" + response.statusCode() + "]" + response.jsonPath().get(); } else { return "[" + response.statusCode() + "]" + response.htmlPath().get(); } }
@Test public void testGetVersion() throws Exception { Response r = given().accept(APPLICATION_JSON_UTF8_VALUE) // .expect() // .statusCode(200).log().ifError() // .with().port(localServerPort) // .get(getVersionPrefix() + "/version"); r.then() // .statusCode(HttpStatus.OK.value()).log().ifError() // .contentType(APPLICATION_JSON_UTF8_VALUE); // We don't care what values are returned as long as they are filled. JsonPath jsonPathEvaluator = r.jsonPath(); assertThat(jsonPathEvaluator.get("version"), allOf(notNullValue(), not(equalTo(VersionDto.N_A)))); assertThat(jsonPathEvaluator.get("commit"), allOf(notNullValue(), not(equalTo(VersionDto.N_A)))); assertThat(jsonPathEvaluator.get("time"), allOf(notNullValue(), not(equalTo(VersionDto.N_A)))); } }