com.alibaba.fastjson.JSONPath类的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(6.2k)|赞(0)|评价(0)|浏览(561)

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

JSONPath介绍

暂无

代码示例

代码示例来源:origin: alibaba/fastjson

public static Object eval(Object rootObject, String path) {
  JSONPath jsonpath = compile(path);
  return jsonpath.eval(rootObject);
}

代码示例来源:origin: alibaba/fastjson

public boolean set(Object rootObject, Object value) {
  return set(rootObject, value, true);
}

代码示例来源:origin: alibaba/fastjson

public static JSONPath compile(String path) {
  if (path == null) {
    throw new JSONPathException("jsonpath can not be null");
  }
  
  JSONPath jsonpath = pathCache.get(path);
  if (jsonpath == null) {
    jsonpath = new JSONPath(path);
    if (pathCache.size() < 1024) {
      pathCache.putIfAbsent(path, jsonpath);
      jsonpath = pathCache.get(path);
    }
  }
  return jsonpath;
}

代码示例来源:origin: alibaba/fastjson

public static int size(Object rootObject, String path) {
  JSONPath jsonpath = compile(path);
  Object result = jsonpath.eval(rootObject);
  return jsonpath.evalSize(result);
}

代码示例来源:origin: alibaba/fastjson

public static boolean contains(Object rootObject, String path) {
  if (rootObject == null) {
    return false;
  }
  JSONPath jsonpath = compile(path);
  return jsonpath.contains(rootObject);
}

代码示例来源:origin: alibaba/fastjson

/**
 * Compile jsonPath and use it to extract keySet or field names from rootObject.
 * 
 * @param rootObject Can be a map or custom object. Array and Collection are not supported.
 * @param path JSONPath string to be compiled.
 * @return Set of keys, or <code>null</code> if not supported.
 */
public static Set<?> keySet(Object rootObject, String path) {
  JSONPath jsonpath = compile(path);
  Object result = jsonpath.eval(rootObject);
  return jsonpath.evalKeySet(result);
}

代码示例来源:origin: xtuhcy/gecco

public static void main(String[] args) {
    Object json = JSON.parse("{ads:[{id:1},{id:2}],test:'test111'}");
    Object src = com.alibaba.fastjson.JSONPath.eval(json, "$.ads");
    if (src instanceof String) {
      src = JSON.parse(src.toString());
    }
    System.out.println(src);
    Object src2 = com.alibaba.fastjson.JSONPath.eval(json, "$.test");
    if (src2 instanceof String) {
      src2 = JSON.parse(src2.toString());
    }
    System.out.println(src2);
  }
}

代码示例来源:origin: sd4324530/fastweixin

JSONObject jsonObject = JSONUtil.getJSONFromString(r.getErrmsg());
List buttonList = (List) JSONPath.eval(jsonObject, "$.menu.button");
if (CollectionUtil.isNotEmpty(buttonList)) {
  for (Object button : buttonList) {
    List subList = (List) JSONPath.eval(button, "$.sub_button");
    if (CollectionUtil.isNotEmpty(subList)) {
      for (Object sub : subList) {
        Object type = JSONPath.eval(sub, "$.type");
        JSONPath.set(sub, "$.type", type.toString().toUpperCase());
      Object type = JSONPath.eval(button, "$.type");
      JSONPath.set(button, "$.type", type.toString().toUpperCase());

代码示例来源:origin: alibaba/fastjson

@SuppressWarnings("rawtypes")
public boolean containsValue(Object rootObject, Object value) {
  Object currentObject = eval(rootObject);
  if (currentObject == value) {
    return true;
  }
  if (currentObject == null) {
    return false;
  }
  if (currentObject instanceof Iterable) {
    Iterator it = ((Iterable) currentObject).iterator();
    while (it.hasNext()) {
      Object item = it.next();
      if (eq(item, value)) {
        return true;
      }
    }
    return false;
  }
  return eq(currentObject, value);
}

代码示例来源:origin: alibaba/fastjson

init();
  return this.eval(root);

代码示例来源:origin: alibaba/fastjson

public static boolean set(Object rootObject, String path, Object value) {
  JSONPath jsonpath = compile(path);
  return jsonpath.set(rootObject, value);
}

代码示例来源:origin: alibaba/fastjson

public static boolean containsValue(Object rootObject, String path, Object value) {
  JSONPath jsonpath = compile(path);
  return jsonpath.containsValue(rootObject, value);
}

代码示例来源:origin: alibaba/fastjson

public static void arrayAdd(Object rootObject, String path, Object... values) {
  JSONPath jsonpath = compile(path);
  jsonpath.arrayAdd(rootObject, values);
}

代码示例来源:origin: alibaba/fastjson

public static boolean remove(Object root, String path) {
  JSONPath jsonpath = compile(path);
  return jsonpath.remove(root);
}

代码示例来源:origin: alibaba/fastjson

/**
 * @since 1.2.51
 * @param json
 * @param path
 * @return
 */
public static Object extract(String json, String path, ParserConfig config, int features, Feature... optionFeatures) {
  features |= Feature.OrderedField.mask;
  DefaultJSONParser parser = new DefaultJSONParser(json, config, features);
  JSONPath jsonPath = compile(path);
  Object result = jsonPath.extract(parser);
  parser.lexer.close();
  return result;
}

代码示例来源:origin: virjar/vscrawler

public AbstractSelectable jsonPath(String jsonPathStr) {
  // FastJson 内部会缓存1024个规则,所以本身应该也会有缓存,对于JsonPath的规则缓存问题,可以先不用考虑了
  return jsonPath(JSONPath.compile(jsonPathStr));
}

代码示例来源:origin: Vedenin/useful-java-links

public  static void main(String[] args) {
  // init class
  Place place = new Place();
  place.setName("World");
  Human human = new Human();
  human.setMessage("Hi");
  human.setPlace(place);
  // convert to json and from json
  String jsonString = JSON.toJSONString(human);
  Human newHuman = JSON.parseObject(jsonString, Human.class);
  // use eval to get info
  Object message = JSONPath.eval(newHuman, "$.message");
  Object world = JSONPath.eval(newHuman, "$.place.name");
  System.out.println(message + " " + world); // print Hi World
}

代码示例来源:origin: sd4324530/fastweixin

JSONObject jsonObject = JSONUtil.getJSONFromString(r.getErrmsg());
List buttonList = (List) JSONPath.eval(jsonObject, "$.menu.button");
if (CollectionUtil.isNotEmpty(buttonList)) {
  for (Object button : buttonList) {
    List subList = (List) JSONPath.eval(button, "$.sub_button");
    if (CollectionUtil.isNotEmpty(subList)) {
      for (Object sub : subList) {
        Object type = JSONPath.eval(sub, "$.type");
        JSONPath.set(sub, "$.type", type.toString().toUpperCase());
      Object type = JSONPath.eval(button, "$.type");
      JSONPath.set(button, "$.type", type.toString().toUpperCase());

代码示例来源:origin: com.alibaba/fastjson

public static int size(Object rootObject, String path) {
  JSONPath jsonpath = compile(path);
  Object result = jsonpath.eval(rootObject);
  return jsonpath.evalSize(result);
}

代码示例来源:origin: com.alibaba/fastjson

/**
 * Compile jsonPath and use it to extract keySet or field names from rootObject.
 * 
 * @param rootObject Can be a map or custom object. Array and Collection are not supported.
 * @param path JSONPath string to be compiled.
 * @return Set of keys, or <code>null</code> if not supported.
 */
public static Set<?> keySet(Object rootObject, String path) {
  JSONPath jsonpath = compile(path);
  Object result = jsonpath.eval(rootObject);
  return jsonpath.evalKeySet(result);
}

相关文章

微信公众号

最新文章

更多