mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
feature: json的getByPath方法新增更为通用的指定出参类型重载
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* JSON路径单元测试
|
||||
*
|
||||
@@ -27,4 +31,23 @@ public class JSONPathTest {
|
||||
Long accountId = JSONUtil.getByPath(json, "$.accountId", 0L);
|
||||
assertEquals(111L, accountId.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByPathTest3(){
|
||||
String str = "[{'accountId':1},{'accountId':2},{'accountId':3}]";
|
||||
JSON json = JSONUtil.parse(str);
|
||||
// 返回指定泛型的对象 List<Long>
|
||||
List<Long> accountIds = json.getByPath("$.accountId", new TypeReference<List<Long>>() {
|
||||
});
|
||||
assertNotNull(accountIds);
|
||||
assertArrayEquals(new Long[]{1L, 2L, 3L}, accountIds.toArray());
|
||||
|
||||
str = "{\"accountInfos\": [{\"accountId\":1},{\"accountId\":2},{\"accountId\":3}]}";
|
||||
json = JSONUtil.parse(str);
|
||||
// 返回指定泛型的对象 List<Long>
|
||||
accountIds = json.getByPath("$.accountInfos.accountId", new TypeReference<List<Long>>() {
|
||||
});
|
||||
assertNotNull(accountIds);
|
||||
assertArrayEquals(new Long[]{1L, 2L, 3L}, accountIds.toArray());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user