1212import java .io .UnsupportedEncodingException ;
1313import java .lang .management .ManagementFactory ;
1414import java .net .InetAddress ;
15+ import java .net .URLDecoder ;
1516import java .net .URLEncoder ;
17+ import java .nio .charset .StandardCharsets ;
1618import java .sql .Connection ;
1719import java .sql .SQLException ;
1820import java .sql .Savepoint ;
@@ -1778,15 +1780,17 @@ public static <V extends Object> V getValue(JSONObject parent, String[] pathKeys
17781780 }
17791781
17801782 //逐层到达child的直接容器JSONObject parent
1781- final int last = pathKeys .length - 1 ;
1783+ int last = pathKeys .length - 1 ;
17821784 for (int i = 0 ; i < last ; i ++) {//一步一步到达指定位置
17831785 if (parent == null ) {//不存在或路径错误(中间的key对应value不是JSONObject)
17841786 break ;
17851787 }
1786- parent = getJSONObject (parent , pathKeys [i ]);
1788+
1789+ String k = getDecodedKey (pathKeys [i ]);
1790+ parent = getJSONObject (parent , k );
17871791 }
17881792
1789- return parent == null ? null : (V ) parent .get (pathKeys [last ]);
1793+ return parent == null ? null : (V ) parent .get (getDecodedKey ( pathKeys [last ]) );
17901794 }
17911795
17921796
@@ -1912,18 +1916,21 @@ public Object getValueByPath(String valuePath) {
19121916 }
19131917
19141918 //逐层到达targetKey的直接容器JSONObject parent
1915- if (keys != null && keys .length > 1 ) {
1916- for (int i = 0 ; i < keys .length - 1 ; i ++) {//一步一步到达指定位置parentPath
1919+ int last = keys == null ? -1 : keys .length - 1 ;
1920+ if (last >= 1 ) {
1921+ for (int i = 0 ; i < last ; i ++) {//一步一步到达指定位置parentPath
19171922 if (parent == null ) {//不存在或路径错误(中间的key对应value不是JSONObject)
19181923 break ;
19191924 }
1920- parent = getJSONObject (parent , keys [i ]);
1925+
1926+ String k = getDecodedKey (keys [i ]);
1927+ parent = getJSONObject (parent , k );
19211928 }
19221929 }
19231930
19241931 if (parent != null ) {
19251932 Log .i (TAG , "getValueByPath >> get from queryResultMap >> return parent.get(keys[keys.length - 1]);" );
1926- target = keys == null || keys . length <= 0 ? parent : parent .get (keys [keys . length - 1 ] ); //值为null应该报错NotExistExeption,一般都是id关联,不可为null,否则可能绕过安全机制
1933+ target = last < 0 ? parent : parent .get (getDecodedKey ( keys [last ]) ); //值为null应该报错NotExistExeption,一般都是id关联,不可为null,否则可能绕过安全机制
19271934 if (target != null ) {
19281935 Log .i (TAG , "getValueByPath >> getValue >> return target = " + target );
19291936 return target ;
@@ -1942,6 +1949,18 @@ public Object getValueByPath(String valuePath) {
19421949 return null ;
19431950 }
19441951
1952+ /**解码 引用赋值 路径中的 key,支持把 URL encode 后的值,转为 decode 后的原始值,例如 %2Fuser%2Flist -> /user/list ; %7B%7D -> []
1953+ * @param key
1954+ * @return
1955+ */
1956+ public static String getDecodedKey (String key ) {
1957+ try {
1958+ return URLDecoder .decode (key , StandardCharsets .UTF_8 );
1959+ } catch (Throwable e ) {
1960+ return key ;
1961+ }
1962+ }
1963+
19451964 //依赖引用关系 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
19461965
19471966
0 commit comments