diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/JSON.java b/hutool-json/src/main/java/org/dromara/hutool/json/JSON.java index aec05fac8..1042de29e 100644 --- a/hutool-json/src/main/java/org/dromara/hutool/json/JSON.java +++ b/hutool-json/src/main/java/org/dromara/hutool/json/JSON.java @@ -122,7 +122,7 @@ public interface JSON extends Converter, Cloneable, Serializable { * @since 4.0.6 */ @SuppressWarnings("unchecked") - default T getByPath(final String expression, final Class resultType){ + default T getByPath(final String expression, final Type resultType){ return (T) config().getConverter().convert(resultType, getByPath(expression)); } diff --git a/hutool-json/src/test/java/org/dromara/hutool/json/Pr3067Test.java b/hutool-json/src/test/java/org/dromara/hutool/json/Pr3067Test.java new file mode 100755 index 000000000..4c3f359d9 --- /dev/null +++ b/hutool-json/src/test/java/org/dromara/hutool/json/Pr3067Test.java @@ -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 resultList = json.getByPath("testUserList[1].testArray", + new TypeReference>() {}); + + 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; + } +} diff --git a/hutool-json/src/test/resources/test_json_path_001.json b/hutool-json/src/test/resources/test_json_path_001.json new file mode 100755 index 000000000..97c10b68c --- /dev/null +++ b/hutool-json/src/test/resources/test_json_path_001.json @@ -0,0 +1,19 @@ +{ + "testUserList": [ + { + "testAbc": "123" + }, + { + "testArray": [ + { + "username": "a", + "password": "a-password" + }, + { + "username": "b", + "password": "b-password" + } + ] + } + ] +}