This commit is contained in:
Looly 2023-04-18 11:17:32 +08:00
parent 315890ddc2
commit f66b5fe55e
3 changed files with 52 additions and 1 deletions

View File

@ -122,7 +122,7 @@ public interface JSON extends Converter, Cloneable, Serializable {
* @since 4.0.6
*/
@SuppressWarnings("unchecked")
default <T> T getByPath(final String expression, final Class<T> resultType){
default <T> T getByPath(final String expression, final Type resultType){
return (T) config().getConverter().convert(resultType, getByPath(expression));
}

View File

@ -0,0 +1,32 @@
package org.dromara.hutool.json;
import lombok.Data;
import org.dromara.hutool.core.io.resource.ResourceUtil;
import org.dromara.hutool.core.reflect.TypeReference;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
public class Pr3067Test {
@Test
public void getListByPathTest1() {
final JSONObject json = JSONUtil.parseObj(ResourceUtil.readUtf8Str("test_json_path_001.json"));
final List<TestUser> resultList = json.getByPath("testUserList[1].testArray",
new TypeReference<List<TestUser>>() {});
Assertions.assertNotNull(resultList);
Assertions.assertEquals(2, resultList.size());
Assertions.assertEquals("a", resultList.get(0).getUsername());
Assertions.assertEquals("a-password", resultList.get(0).getPassword());
Assertions.assertEquals("b", resultList.get(1).getUsername());
Assertions.assertEquals("b-password", resultList.get(1).getPassword());
}
@Data
public static class TestUser {
private String username;
private String password;
}
}

View File

@ -0,0 +1,19 @@
{
"testUserList": [
{
"testAbc": "123"
},
{
"testArray": [
{
"username": "a",
"password": "a-password"
},
{
"username": "b",
"password": "b-password"
}
]
}
]
}