public static int size(Object rootObject, String path) { JSONPath jsonpath = compile(path); Object result = jsonpath.eval(rootObject); return jsonpath.evalSize(result); }
/** * 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); }
public static Object eval(Object rootObject, String path) { JSONPath jsonpath = compile(path); return jsonpath.eval(rootObject); }
/** * @since 1.2.9 * @param json * @param path * @return */ public static Object read(String json, String path) { return compile(path) .eval( JSON.parse(json) ); }
public static int size(Object rootObject, String path) { JSONPath jsonpath = compile(path); Object result = jsonpath.eval(rootObject); return jsonpath.evalSize(result); }
/** * 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); }
@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); }
public static Object eval(Object rootObject, String path) { JSONPath jsonpath = compile(path); return jsonpath.eval(rootObject); }
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); } }
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 }
/** * @since 1.2.9 * @param json * @param path * @return */ public static Object read(String json, String path) { return compile(path) .eval( JSON.parse(json) ); }
@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); }
Object src = com.alibaba.fastjson.JSONPath.eval(json, jsonPath);
String jsonPath = JSONPath.value(); Object src = com.alibaba.fastjson.JSONPath.eval(json, jsonPath); boolean isArray = type.isArray(); boolean isList = ReflectUtils.haveSuperType(type, List.class);// 是List类型
return this.eval(root);
return this.eval(root);
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());
@Override public OAuthUser getOAuthUser(Token accessToken) { OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); this.signRequest(accessToken, request); Response response = request.send(); OAuthUser oAuthUser = new OAuthUser(); oAuthUser.setoAuthType(getoAuthType()); Object result = JSON.parse(response.getBody()); oAuthUser.setoAuthId(JSONPath.eval(result, "$.id").toString()); oAuthUser.setUserName(JSONPath.eval(result, "$.login").toString()); return oAuthUser; }
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());