本文整理了Java中com.jayway.jsonpath.JsonPath
类的一些代码示例,展示了JsonPath
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonPath
类的具体详情如下:
包路径:com.jayway.jsonpath.JsonPath
类名称:JsonPath
[英]JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPath is available in many programming languages such as Javascript, Python and PHP.
JsonPath allows you to compile a json path string to use it many times or to compile and apply in one single on demand operation.
Given the Json document:
String 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
}
],
"bicycle":
{
"color": "red",
"price": 19.95
}
}
}";
A JsonPath can be compiled and used as shown:
JsonPath path = JsonPath.compile("$.store.book[1]"); List<Object> books = path.read(json);
Or:
List<Object> authors = JsonPath.read(json, "$.store.book[*].author")
If the json path returns a single value (is definite):String author = JsonPath.read(json, "$.store.book[1].author")
[中]JsonPath对于JSON就像XPATH对于XML一样,是提取给定文档部分的简单方法。JsonPath有多种编程语言,如Javascript、Python和PHP。
JsonPath允许您编译json路径字符串以多次使用它,或者在一次按需操作中编译并应用它。
给定Json文档:
String 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
}
],
"bicycle":
{
"color": "red",
"price": 19.95
}
}
}";
可以编译并使用JsonPath,如图所示:JsonPath path = JsonPath.compile("$.store.book[1]"); List<Object> books = path.read(json);
或:List<Object> authors = JsonPath.read(json, "$.store.book[*].author")
如果json路径返回单个值(确定):String author = JsonPath.read(json, "$.store.book[1].author")
代码示例来源:origin: Vedenin/useful-java-links
public static void main(String[] args) {
List<String> authors = JsonPath.read(json, "$.store.book[*].author");
System.out.println("authors: " + authors); // print ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
List<Map<String, Object>> expensiveBooks = JsonPath
.using(Configuration.defaultConfiguration())
.parse(json)
.read("$.store.book[?(@.price > 22)].title", List.class);
System.out.println(expensiveBooks); // print ["Hello, Middle-earth! "]
System.out.println();
String jsonHiWorld = "{\"message\":\"Hi\",\"place\":{\"name\":\"World!\"}}\"";
String message = JsonPath.read(jsonHiWorld, "$.message");
String place = JsonPath.read(jsonHiWorld, "$.place.name");
System.out.println(message + " " + place); // print "Hi World!"
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Variant of {@link #evaluateJsonPath(String)} with a target type.
* This can be useful for matching numbers reliably for example coercing an
* integer into a double.
* @param content the content to evaluate against
* @return the result of the evaluation
* @throws AssertionError if the evaluation fails
*/
public Object evaluateJsonPath(String content, Class<?> targetType) {
try {
return JsonPath.parse(content).read(this.expression, targetType);
}
catch (Throwable ex) {
String message = "No value at JSON path \"" + this.expression + "\"";
throw new AssertionError(message, ex);
}
}
代码示例来源:origin: apache/incubator-druid
@Override
public Function<JsonNode, Object> makeJsonPathExtractor(final String expr)
{
final JsonPath jsonPath = JsonPath.compile(expr);
return node -> valueConversionFunction(jsonPath.read(node, JSONPATH_CONFIGURATION));
}
代码示例来源:origin: json-path/JsonPath
/**
* @see JsonPath#isDefinite()
*/
public static boolean isPathDefinite(String path) {
return compile(path).isDefinite();
}
代码示例来源:origin: json-path/JsonPath
@Test(expected = PathNotFoundException.class)
public void missing_prop() {
//Object read = JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.THROW_ON_MISSING_PROPERTY)).parse(DOCUMENT).read("$.store.book[*].fooBar");
//Object read = JsonPath.using(Configuration.defaultConfiguration()).parse(DOCUMENT).read("$.store.book[*].fooBar");
Object read2 = JsonPath.using(Configuration.defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES)).parse(DOCUMENT).read("$.store.book[*].fooBar.not");
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Evaluate the JSON path and return the resulting value.
* @param content the content to evaluate against
* @return the result of the evaluation
* @throws AssertionError if the evaluation fails
*/
@Nullable
public Object evaluateJsonPath(String content) {
try {
return this.jsonPath.read(content);
}
catch (Throwable ex) {
throw new AssertionError("No value at JSON path \"" + this.expression + "\"", ex);
}
}
代码示例来源:origin: json-path/JsonPath
@Test
public void a_definite_path_can_be_returned_as_list() {
Configuration conf = Configuration.builder().options(ALWAYS_RETURN_LIST).build();
assertThat((List)using(conf).parse("{\"foo\" : \"bar\"}").read("$.foo")).isInstanceOf(List.class);
assertThat((List)using(conf).parse("{\"foo\": null}").read("$.foo")).isInstanceOf(List.class);
assertThat((List)using(conf).parse("{\"foo\": [1, 4, 8]}").read("$.foo")).asList()
.containsExactly(Arrays.asList(1, 4, 8));
}
代码示例来源:origin: json-path/JsonPath
/**
* {@inheritDoc}
*/
public JsonAsserter assertNotDefined(String path) {
try {
Configuration c = Configuration.defaultConfiguration();
JsonPath.using(c).parse(jsonObject).read(path);
throw new AssertionError(format("Document contains the path <%s> but was expected not to.", path));
} catch (PathNotFoundException e) {
}
return this;
}
代码示例来源:origin: json-path/JsonPath
@Test
public void an_filter_can_update() {
Object o = parse(JSON_DOCUMENT).set("$.store.book[?(@.display-price)].display-price", 1).json();
List<Integer> result = parse(o).read("$.store.book[?(@.display-price)].display-price");
assertThat(result).containsExactly(1, 1, 1, 1);
}
代码示例来源:origin: json-path/JsonPath
@Test
public void an_array_slice_can_be_updated() {
List<String> res = parse(JSON_DOCUMENT).set("$.store.book[0:2]", "a").read("$.store.book[0:2]");
assertThat(res).containsExactly("a", "a");
}
代码示例来源:origin: json-path/JsonPath
/**
* Applies this JsonPath to the provided json input stream
*
* @param jsonInputStream input stream to read from
* @param <T> expected return type
* @return list of objects matched by the given path
* @throws IOException
*/
@SuppressWarnings({"unchecked"})
public <T> T read(InputStream jsonInputStream) throws IOException {
return read(jsonInputStream, Configuration.defaultConfiguration());
}
代码示例来源:origin: json-path/JsonPath
@Test
public void last_token_defaults_to_null() {
Configuration configuration = Configuration.builder().options(Option.DEFAULT_PATH_LEAF_TO_NULL).build();
assertNull(JsonPath.parse(DOCUMENT, configuration).read("$.children[2].age"));
}
代码示例来源:origin: json-path/JsonPath
@Test
public void keys_in_root_containing_map_can_be_renamed(){
Object o = parse(JSON_DOCUMENT).renameKey("$", "store", "new-store").json();
List<Object> result = parse(o).read("$.new-store[*]");
assertThat(result).isNotEmpty();
}
代码示例来源:origin: json-path/JsonPath
@Test
public void multi_prop_delete() {
List<Map<String, Object>> res = parse(JSON_DOCUMENT).delete("$.store.book[*]['author', 'category']").read("$.store.book[*]['author', 'category']");
assertThat(res).containsExactly(EMPTY_MAP, EMPTY_MAP, EMPTY_MAP, EMPTY_MAP);
}
代码示例来源:origin: json-path/JsonPath
@Test(expected = PathNotFoundException.class)
public void issue_22() throws Exception {
Configuration configuration = Configuration.defaultConfiguration();
String json = "{\"a\":{\"b\":1,\"c\":2}}";
JsonPath.parse(json, configuration).read("a.d");
}
代码示例来源:origin: json-path/JsonPath
@Test
public void issue_143() {
String json = "{ \"foo\": { \"bar\" : \"val\" }, \"moo\": { \"cow\" : \"val\" } }";
Configuration configuration = Configuration.builder().options( Option.AS_PATH_LIST ).build();
List<String> pathList = JsonPath.using(configuration).parse(json).read(JsonPath.compile("$.*.bar"));
assertThat(pathList).containsExactly("$['foo']['bar']");
}
代码示例来源:origin: json-path/JsonPath
@Test
public void isbn_is_defaulted_when_option_is_provided() {
List<String> result1 = JsonPath.using(JSON_SMART_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book.*.isbn");
assertThat(result1).containsExactly("0-553-21311-3","0-395-19395-8");
List<String> result2 = JsonPath.using(JSON_SMART_CONFIGURATION.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)).parse(JSON_DOCUMENT).read("$.store.book.*.isbn");
assertThat(result2).containsExactly(null, null, "0-553-21311-3", "0-395-19395-8");
}
}
代码示例来源:origin: json-path/JsonPath
@Test
public void add_to_object() {
Object res = parse(JSON_DOCUMENT).put("$.store.book[0]", "new-key", "new-value").read("$.store.book[0].new-key");
assertThat(res).isEqualTo("new-value");
}
代码示例来源:origin: code4craft/webmagic
public JsonPathSelector(String jsonPathStr) {
this.jsonPathStr = jsonPathStr;
this.jsonPath = JsonPath.compile(this.jsonPathStr);
}
代码示例来源:origin: json-path/JsonPath
@Test
public void item_can_be_added_to_root_array() {
List<Integer> model = new LinkedList<Integer>();
model.add(1);
model.add(2);
List<Integer> ints = parse(model).add("$", 3).read("$");
assertThat(ints).containsExactly(1,2,3);
}
内容来源于网络,如有侵权,请联系作者删除!