大家好,又见面了,我是你们的朋友全栈君。
/**
* @author itguang
* @create 2017-12-10 10:03
**/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class JSONpathControllerTest {
@Test
public void test() {
User user = new User(“itguang”, “123456”, “123@qq.com”);
String username = (String) JSONPath.eval(user, “$.username”);
log.info(“$.username = {}”, username);
Entity entity = new Entity(123, user);
User user1 = (User) JSONPath.eval(entity, “$.value”);
log.info(“user={}”, user1.toString());
}
@Test
public void test2() {
User user = new User(“itguang”, “123456”, “123@qq.com”);
Entity entity = new Entity(123, user);
//判断entity中是否有 data
boolean contains = JSONPath.contains(entity, “$.data”);
Assert.assertTrue(contains);
//判断 entity.data.username 属性值是否为 itguang
boolean containsValue = JSONPath.containsValue(entity, “$.data.username”, “itguang”);
Assert.assertTrue(containsValue);
Assert.assertEquals(2, JSONPath.size(entity, “$”));
}
@Test
public void test3() {
List entities = new ArrayList();
entities.add(new Entity(“逻辑”));
entities.add(new Entity(“叶文杰”));
entities.add(new Entity(“程心”));
//返回集合中多个元素
List names = (List) JSONPath.eval(entities, “$.name”);
log.info(“返回集合中多个元素names={}”, names);
//返回下标 0 和 2 的元素
List result = (List) JSONPath.eval(entities, “[0,2]”);
log.info(“返回下标 0 和 2 的元素={}”, result);
// 返回下标从0到2的元素
List result2 = (List) JSONPath.eval(entities, “[0:2]”);
log.info(“返回下标从0到2的元素={}”, result2);
}
@Test
public void test4() {
List entities = new ArrayList();
entities.add(new Entity(1001, “逻辑”));
entities.add(new Entity(1002, “程心”));
entities.add(new Entity(1003, “叶文杰”));
entities.add(new Entity(1004, null));
//通过条件过滤,返回集合的子集
List result = (List) JSONPath.eval(entities, “[id in (1001)]”);
log.info(“通过条件过滤,返回集合的子集={}”, result);
}
/**
* 使用JSONPrase 解析JSON字符串或者Object对象
*
* read(String json, String path)//直接使用json字符串匹配
*
* eval(Object rootObject, String path) //直接使用 对象匹配
*
*
* {“store”:{“bicycle”:{“color”:”red”,”price”:19.95},”book”:[{“author”:”Nigel Rees”,”price”:8.95,”category”:”reference”,”title”:”Sayings of the Century”},{“author”:”Evelyn Waugh”,”price”:12.99,”isbn”:”0-553-21311-3″,”category”:”fiction”,”title”:”Sword of Honour”}]}}
*/
@Test
public void test5() {
String jsonStr = “{\n” +
” \”store\”: {\n” +
” \”bicycle\”: {\n” +
” \”color\”: \”red\”,\n” +
” \”price\”: 19.95\n” +
” },\n” +
” \”book\”: [\n” +
” {\n” +
” \”author\”: \”刘慈欣\”,\n” +
” \”price\”: 8.95,\n” +
” \”category\”: \”科幻\”,\n” +
” \”title\”: \”三体\”\n” +
” },\n” +
” {\n” +
” \”author\”: \”itguang\”,\n” +
” \”price\”: 12.99,\n” +
” \”category\”: \”编程语言\”,\n” +
” \”title\”: \”go语言实战\”\n” +
” }\n” +
” ]\n” +
” }\n” +
“}”;
JSONObject jsonObject = JSON.parseObject(jsonStr);
log.info(jsonObject.toString());
//得到所有的书
List books = (List) JSONPath.eval(jsonObject, “$.store.book”);
log.info(“books={}”, books);
//得到所有的书名
List titles = (List) JSONPath.eval(jsonObject, “$.store.book.title”);
log.info(“titles={}”, titles);
//第一本书title
String title = (String) JSONPath.read(jsonStr, “$.store.book[0].title”);
log.info(“title={}”, title);
//price大于10元的book
List list = (List) JSONPath.read(jsonStr, “$.store.book[price > 10]”);
log.info(“price大于10元的book={}”,list);
//price大于10元的title
List list2 =(List) JSONPath.read(jsonStr, “$.store.book[price > 10].title”);
log.info(“price大于10元的title={}”,list2);
//category(类别)为科幻的book
List list3 = (List) JSONPath.read(jsonStr,”$.store.book[category = ‘科幻’]”);
log.info(“category(类别)为科幻的book={}”,list3);
//bicycle的所有属性值
Collection values = (Collection) JSONPath.eval(jsonObject, “$.store.bicycle.*”);
log.info(“bicycle的所有属性值={}”,values);
//bicycle的color和price属性值
List read =(List) JSONPath.read(jsonStr, “$.store.bicycle[‘color’,’price’]”);
log.info(“bicycle的color和price属性值={}”,read);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/150519.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...