Skip to content

Commit 3da8684

Browse files
committed
Server:新增Table-key1-key2...[]取值
1 parent 0c9928c commit 3da8684

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

APIJSON-Java-Server/APIJSON-Eclipse/src/main/java/zuo/biao/apijson/StringUtil.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -737,23 +737,35 @@ public static String[] split(String s) {
737737
return split(s, null);
738738
}
739739
/**将s用split分割成String[]
740+
* trim = true;
740741
* @param s
741742
* @param split
742743
* @return
743744
*/
744745
public static String[] split(String s, String split) {
746+
return split(s, split, true);
747+
}
748+
/**将s用split分割成String[]
749+
* @param s
750+
* @param split
751+
* @param trim 去掉前后两端的split
752+
* @return
753+
*/
754+
public static String[] split(String s, String split, boolean trim) {
745755
s = getString(s);
746756
if (s.isEmpty()) {
747757
return null;
748758
}
749759
if (isNotEmpty(split, false) == false) {
750760
split = ",";
751761
}
752-
while (s.startsWith(split)) {
753-
s = s.substring(split.length());
754-
}
755-
while (s.endsWith(split)) {
756-
s = s.substring(0, s.length() - split.length());
762+
if (trim) {
763+
while (s.startsWith(split)) {
764+
s = s.substring(split.length());
765+
}
766+
while (s.endsWith(split)) {
767+
s = s.substring(0, s.length() - split.length());
768+
}
757769
}
758770
return s.contains(split) ? s.split(split) : new String[]{s};
759771
}

APIJSON-Java-Server/APIJSON-Eclipse/src/main/java/zuo/biao/apijson/server/Parser.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -643,11 +643,13 @@ private JSONArray getArray(String parentPath, String name, final JSONObject requ
643643

644644

645645
//key[]:{Table:{}}中key equals Table时 提取Table
646-
boolean isContainer = true;
647646
int index = name == null ? -1 : name.lastIndexOf("[]");
648-
String table = index <= 0 ? null : Pair.parseEntry(name.substring(0, index), true).getKey();
649-
if (JSONRequest.isTableKey(table) && request.containsKey(table)) {
650-
isContainer = false;
647+
String childPath = index <= 0 ? null : Pair.parseEntry(name.substring(0, index), true).getKey(); // Table-key1-key2...
648+
649+
//判断第一个key,即Table是否存在,如果存在就提取
650+
String[] childKeys = StringUtil.split(childPath, "-", false);
651+
if (childKeys == null || childKeys.length <= 0 || request.containsKey(childKeys[0]) == false) {
652+
childKeys = null;
651653
}
652654

653655

@@ -662,7 +664,7 @@ private JSONArray getArray(String parentPath, String name, final JSONObject requ
662664
break;
663665
}
664666
//key[]:{Table:{}}中key equals Table时 提取Table
665-
response.add(isContainer ? parent : parent.getJSONObject(table) );
667+
response.add(getValue(parent, childKeys)); //null有意义
666668
}
667669
//Table>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
668670

@@ -676,6 +678,29 @@ private JSONArray getArray(String parentPath, String name, final JSONObject requ
676678
}
677679

678680

681+
/**根据路径取值
682+
* @param parent
683+
* @param pathKeys
684+
* @return
685+
*/
686+
private static Object getValue(JSONObject parent, String[] pathKeys) {
687+
if (parent == null || pathKeys == null || pathKeys.length <= 0) {
688+
Log.w(TAG, "getChild parent == null || pathKeys == null || pathKeys.length <= 0 >> return parent;");
689+
return parent;
690+
}
691+
692+
//逐层到达child的直接容器JSONObject parent
693+
final int last = pathKeys.length - 1;
694+
for (int i = 0; i < last; i++) {//一步一步到达指定位置
695+
if (parent == null) {//不存在或路径错误(中间的key对应value不是JSONObject)
696+
break;
697+
}
698+
parent = getJSONObject(parent, pathKeys[i]);
699+
}
700+
701+
return parent == null ? null : parent.get(pathKeys[last]);
702+
}
703+
679704

680705
/**获取被依赖引用的key的路径, 实时替换[] -> []/i
681706
* @param parentPath

0 commit comments

Comments
 (0)