com.jayway.jsonpath.JsonPath.read()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(6.6k)|赞(0)|评价(0)|浏览(464)

本文整理了Java中com.jayway.jsonpath.JsonPath.read()方法的一些代码示例,展示了JsonPath.read()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonPath.read()方法的具体详情如下:
包路径:com.jayway.jsonpath.JsonPath
类名称:JsonPath
方法名:read

JsonPath.read介绍

[英]Applies this JsonPath to the provided json file
[中]将此JsonPath应用于提供的json文件

代码示例

代码示例来源: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: code4craft/webmagic

@Override
public String select(String text) {
  Object object = jsonPath.read(text);
  if (object == null) {
    return null;
  }
  if (object instanceof List) {
    List list = (List) object;
    if (list != null && list.size() > 0) {
      return toString(list.iterator().next());
    }
  }
  return object.toString();
}

代码示例来源:origin: json-path/JsonPath

/**
 * Applies this JsonPath to the provided json file
 *
 * @param jsonFile file 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(File jsonFile) throws IOException {
  return read(jsonFile, Configuration.defaultConfiguration());
}

代码示例来源: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: apache/incubator-druid

@Override
public Function<GenericRecord, Object> makeJsonPathExtractor(final String expr)
{
 final JsonPath jsonPath = JsonPath.compile(expr);
 return record -> transformValue(jsonPath.read(record, JSONPATH_CONFIGURATION));
}

代码示例来源:origin: apache/incubator-druid

@Override
public Function<Group, Object> makeJsonPathExtractor(String expr)
{
 final JsonPath jsonPath = JsonPath.compile(expr);
 return record -> {
  Object val = jsonPath.read(record, jsonPathConfiguration);
  return finalizeConversion(val);
 };
}

代码示例来源:origin: json-path/JsonPath

@Test
  public void testIssue234() {
    Map<String, String> context = Maps.newHashMap();
    context.put("key", "first");
    Object value = JsonPath.read(context, "concat(\"/\", $.key)");
    assertThat(value).isEqualTo("/first");
    context.put("key", "second");
    value = JsonPath.read(context, "concat(\"/\", $.key)");
    assertThat(value).isEqualTo("/second");
  }
}

代码示例来源:origin: json-path/JsonPath

@Test(expected = PathNotFoundException.class)
public void issue_11b() throws Exception {
  String json = "{ \"foo\" : [] }";
  read(json, "$.foo[0].uri");
}

代码示例来源:origin: json-path/JsonPath

@Test
public void read_store_book_wildcard() throws Exception {
  JsonPath path = JsonPath.compile("$.store.book[*]");
  List<Object> list = path.read(DOCUMENT);
  Assertions.assertThat(list.size()).isEqualTo(4);
}

代码示例来源:origin: json-path/JsonPath

@Test
public void issue_30() throws Exception {
  String json = "{\"foo\" : {\"@id\" : \"123\", \"$\" : \"hello\"}}";
  assertEquals("123", read(json, "foo.@id"));
  assertEquals("hello", read(json, "foo.$"));
}

代码示例来源:origin: json-path/JsonPath

@Test
public void issue_42() {
  String json = "{" +
      "        \"list\": [{" +
      "            \"name\": \"My (String)\" " +
      "        }] " +
      "    }";
  List<Map<String, String>> result = read(json, "$.list[?(@.name == 'My (String)')]");
  assertThat(result).containsExactly(Collections.singletonMap("name", "My (String)"));
}

代码示例来源:origin: json-path/JsonPath

@Test
public void a_document_can_be_scanned_for_property_path() {
  List<String> result = read(DOCUMENT, "$..address.street");
  assertThat(result).containsOnly("fleet street", "Baker street", "Svea gatan", "Söder gatan");
}

代码示例来源:origin: json-path/JsonPath

@Test
public void issue_114_c() {
  String json = "{ \"p\": [\"valp\", \"valq\", \"valr\"] }";
  List<String> result = read(json, "$.p[?(@[0] == 'valp')]");
  assertThat(result).isEmpty();
}

代码示例来源:origin: json-path/JsonPath

@Test
public void map_value_can_be_read_from_array() {
  List<String> result = JsonPath.read(SIMPLE_ARRAY, "$[*].foo");
  assertThat(result).containsOnly("foo-val-0", "foo-val-1");
}

代码示例来源:origin: json-path/JsonPath

@Test
public void get_from_index(){
  List<Integer> result = JsonPath.read(JSON_ARRAY, "$[:3]");
  assertThat(result, Matchers.contains(1,3,5));
}

代码示例来源:origin: json-path/JsonPath

@Test
public void get_between_index_3(){
  List<Integer> result = JsonPath.read(JSON_ARRAY, "$[0:2]");
  assertThat(result, Matchers.contains(1,3));
}

代码示例来源:origin: json-path/JsonPath

@Test
public void get_from_tail_index(){
  List<Integer> result = JsonPath.read(JSON_ARRAY, "$[-3:]");
  assertThat(result, Matchers.contains(8, 13, 20));
}

代码示例来源:origin: json-path/JsonPath

@Test
public void get_indexes(){
  List<Integer> result = JsonPath.read(JSON_ARRAY, "$[0,1,2]");
  assertThat(result, Matchers.contains(1,3,5));
}

代码示例来源:origin: json-path/JsonPath

@Test
public void a_filter_predicate_can_be_evaluated_on_decimal_criteria() {
  List<Map> result = JsonPath.read (ARRAY2, "$[?(@.decimal == 0.1)]");
  assertThat(result).hasSize(1);
  assertThat(result.get(0)).contains(entry("decimal", 0.1));
}

代码示例来源:origin: json-path/JsonPath

@Test
public void read_store_book_author() throws Exception {
  assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.store.book[0,1].author"), hasItems("Nigel Rees", "Evelyn Waugh"));
  assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.store.book[*].author"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"));
  assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.['store'].['book'][*].['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"));
  assertThat(JsonPath.<List<String>>read(DOCUMENT, "$['store']['book'][*]['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"));
  assertThat(JsonPath.<List<String>>read(DOCUMENT, "$['store'].book[*]['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"));
}

相关文章