Search in sources :

Example 11 with JSONPath

use of com.alibaba.fastjson.JSONPath in project fastjson by alibaba.

the class JSONPath_field_access_filter_rlike method test_list_like_left_match.

public void test_list_like_left_match() throws Exception {
    JSONPath path = new JSONPath("$[?(@.name like 'ljw%')]");
    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity(1001, "ljw2083"));
    entities.add(new Entity(1002, "wenshao"));
    entities.add(new Entity(1003, "yakolee"));
    entities.add(new Entity(null, null));
    List<Object> result = (List<Object>) path.eval(entities);
    Assert.assertEquals(1, result.size());
    Assert.assertSame(entities.get(0), result.get(0));
}
Also used : JSONPath(com.alibaba.fastjson.JSONPath) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 12 with JSONPath

use of com.alibaba.fastjson.JSONPath in project fastjson by alibaba.

the class JSONPath_field_access_multi method test_list_map.

public void test_list_map() throws Exception {
    Entity entity = new Entity(123, "wenshao");
    JSONPath path = new JSONPath("$['id','name']");
    List<Object> result = (List<Object>) path.eval(entity);
    Assert.assertSame(entity.getId(), result.get(0));
    Assert.assertSame(entity.getName(), result.get(1));
}
Also used : JSONPath(com.alibaba.fastjson.JSONPath) List(java.util.List)

Example 13 with JSONPath

use of com.alibaba.fastjson.JSONPath in project fastjson by alibaba.

the class JSONPath_field_wildcard method test_list_map_none_root.

public void test_list_map_none_root() throws Exception {
    JSONPath path = new JSONPath("*");
    Entity entity = new Entity(123, "wenshao");
    List<Object> fieldValues = (List<Object>) path.eval(entity);
    Assert.assertSame(entity.getId(), fieldValues.get(0));
    Assert.assertSame(entity.getName(), fieldValues.get(1));
}
Also used : JSONPath(com.alibaba.fastjson.JSONPath) List(java.util.List)

Example 14 with JSONPath

use of com.alibaba.fastjson.JSONPath in project fastjson by alibaba.

the class JSONPath_field_wildcard method test_list_map.

public void test_list_map() throws Exception {
    JSONPath path = new JSONPath("$.*");
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    map.put("id", 123);
    map.put("name", "wenshao");
    Collection<Object> fieldValues = (Collection<Object>) path.eval(map);
    Iterator<Object> it = fieldValues.iterator();
    Assert.assertSame(map.get("id"), it.next());
    Assert.assertSame(map.get("name"), it.next());
}
Also used : JSONPath(com.alibaba.fastjson.JSONPath) Collection(java.util.Collection) LinkedHashMap(java.util.LinkedHashMap)

Example 15 with JSONPath

use of com.alibaba.fastjson.JSONPath in project fastjson by alibaba.

the class JSONPath_list_field method test_list_field.

public void test_list_field() throws Exception {
    JSONPath path = new JSONPath("$.name");
    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity("wenshao"));
    entities.add(new Entity("ljw2083"));
    List<String> names = (List<String>) path.eval(entities);
    Assert.assertSame(entities.get(0).getName(), names.get(0));
    Assert.assertSame(entities.get(1).getName(), names.get(1));
}
Also used : JSONPath(com.alibaba.fastjson.JSONPath) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

JSONPath (com.alibaba.fastjson.JSONPath)96 List (java.util.List)77 ArrayList (java.util.ArrayList)76 HashMap (java.util.HashMap)6 JSONException (com.alibaba.fastjson.JSONException)2 Entity (com.alibaba.json.bvt.path.JSONPath_between_int.Entity)2 JSONObject (com.alibaba.fastjson.JSONObject)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1