Skip to content

Commit e4f988a

Browse files
committed
Server:同步eclipse版至idea版
1 parent 610d60e commit e4f988a

File tree

4 files changed

+136
-97
lines changed

4 files changed

+136
-97
lines changed

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/JSON.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class JSON {
3232
* @return
3333
*/
3434
public static boolean isJsonCorrect(String s) {
35-
// Log.i(TAG, "isJsonCorrect <<<< " + s + " >>>>>>>");
35+
//太长 Log.i(TAG, "isJsonCorrect <<<< " + s + " >>>>>>>");
3636
if (s == null
3737
// || s.equals("[]")
3838
// || s.equals("{}")
@@ -222,7 +222,7 @@ public static boolean isJSONObject(Object obj) {
222222
Log.e(TAG, "isJSONObject catch \n" + e.getMessage());
223223
}
224224
}
225-
225+
226226
return false;
227227
}
228228
/**判断是否为JSONArray
@@ -241,7 +241,7 @@ public static boolean isJSONArray(Object obj) {
241241
Log.e(TAG, "isJSONArray catch \n" + e.getMessage());
242242
}
243243
}
244-
244+
245245
return false;
246246
}
247247

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/JSONResponse.java

+38-26
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public JSONResponse(JSONObject object) {
4646
}
4747

4848
//状态信息,非GET请求获得的信息<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
49-
49+
5050
public static final int STATUS_SUCCEED = 200;
51-
52-
51+
52+
5353
public static final String KEY_ID = "id";
5454
public static final String KEY_STATUS = "status";
5555
public static final String KEY_COUNT = "count";
@@ -91,7 +91,7 @@ public int getTotal() {
9191
public String getMessage() {
9292
return getString(KEY_MESSAGE);
9393
}
94-
94+
9595
/**是否成功
9696
* @return
9797
*/
@@ -112,7 +112,7 @@ public static boolean isSucceed(int status) {
112112
public static boolean isSucceed(JSONResponse response) {
113113
return response != null && response.isSucceed();
114114
}
115-
115+
116116
/**校验服务端是否存在table
117117
* @return
118118
*/
@@ -296,10 +296,10 @@ public static JSONArray getArray(JSONObject object, String key, String className
296296
public JSONArray toArray(String className) {
297297
return toArray(this, className);
298298
}
299-
/**
299+
/**{0:{Table:{}}, 1:{Table:{}}...} 转化为 [{Table:{}}, {Table:{}}]
300300
* array.set(index, isContainer ? value : value.getJSONObject(className));
301301
* @param arrayObject
302-
* @param className className.isEmpty() == false && value.containsKey(className) >> isContainer = true;
302+
* @param className className.equals(Table) ? {Table:{Content}} => {Content}
303303
* @return
304304
*/
305305
public static JSONArray toArray(JSONObject arrayObject, String className) {
@@ -334,7 +334,7 @@ public static JSONArray toArray(JSONObject arrayObject, String className) {
334334
if (value != null) {
335335
try {
336336
index = Integer.valueOf(0 + key);
337-
if (isFirst && className.isEmpty() == false && value.containsKey(className)) {// 判断是否需要提取table
337+
if (isFirst && isTableKey(className) && value.containsKey(className)) {// 判断是否需要提取table
338338
isContainer = false;
339339
}
340340
array.set(index, isContainer ? value : value.getJSONObject(className));
@@ -349,19 +349,20 @@ public static JSONArray toArray(JSONObject arrayObject, String className) {
349349

350350

351351

352-
// /**将Table[]:{0:{Table:{}}, 1:{Table:{}}...} 转化为 tableList:[{}, {}]
352+
// /**
353353
// * @return
354354
// */
355355
// public JSONObject format() {
356356
// return format(this);
357357
// }
358-
/**将Table[]:{0:{Table:{}}, 1:{Table:{}}...} 转化为 tableList:[{}, {}]
358+
/**将Item[]:[{Table:{}}, {Table:{}}...] 或 Item[]:{0:{Table:{}}, 1:{Table:{}}...}
359+
* 转化为 itemList:[{Table:{}}, {Table:{}}],如果 Item.equals(Table),则将 {Table:{Content}} 转化为 {Content}
359360
* @param target
360361
* @param response
361362
* @return
362363
*/
363364
public static JSONObject format(final JSONObject response) {
364-
Log.i(TAG, "format response = \n" + JSON.toJSONString(response));
365+
//太长查看不方便,不如debug Log.i(TAG, "format response = \n" + JSON.toJSONString(response));
365366
if (response == null || response.isEmpty()) {
366367
Log.i(TAG, "format response == null || response.isEmpty() >> return response;");
367368
return response;
@@ -370,19 +371,19 @@ public static JSONObject format(final JSONObject response) {
370371

371372
Set<String> set = response.keySet();
372373
if (set != null) {
373-
374+
374375
Object value;
375376
String arrayKey;
376377
for (String key : set) {
377378
value = response.get(key);
378-
379+
379380
if (value instanceof JSONArray) {//转化JSONArray内部的APIJSON Array
380-
transferredObject.put(getSimpleName(key), format((JSONArray) value));
381+
transferredObject.put(replaceArray(key), format(key, (JSONArray) value));
381382
} else if (value instanceof JSONObject) {//APIJSON Array转为常规JSONArray
382383
if (isArrayKey(key)) {//APIJSON Array转为常规JSONArray
383-
arrayKey = key.substring(0, key.indexOf(KEY_ARRAY));
384+
arrayKey = key.substring(0, key.lastIndexOf(KEY_ARRAY));
384385
transferredObject.put(getArrayKey(getSimpleName(arrayKey))
385-
, format(toArray((JSONObject) value, arrayKey)));//需要将name:alias传至toArray
386+
, format(key, toArray((JSONObject) value, arrayKey)));//需要将name:alias传至toArray
386387
} else {//常规JSONObject,往下一级提取
387388
transferredObject.put(getSimpleName(key), format((JSONObject) value));
388389
}
@@ -392,35 +393,46 @@ public static JSONObject format(final JSONObject response) {
392393
}
393394
}
394395

395-
Log.i(TAG, "format return transferredObject = " + JSON.toJSONString(transferredObject));
396+
//太长查看不方便,不如debug Log.i(TAG, "format return transferredObject = " + JSON.toJSONString(transferredObject));
396397
return transferredObject;
397398
}
398-
399+
399400
/**
400401
* @param responseArray
401402
* @return
402403
*/
403-
public static JSONArray format(final JSONArray responseArray) {
404-
Log.i(TAG, "format responseArray = \n" + JSON.toJSONString(responseArray));
404+
public static JSONArray format(String name, final JSONArray responseArray) {
405+
//太长查看不方便,不如debug Log.i(TAG, "format responseArray = \n" + JSON.toJSONString(responseArray));
405406
if (responseArray == null || responseArray.isEmpty()) {
406407
Log.i(TAG, "format responseArray == null || responseArray.isEmpty() >> return response;");
407408
return responseArray;
408409
}
410+
int index = name == null ? -1 : name.lastIndexOf(KEY_ARRAY);
411+
String className = index < 0 ? "" : name.substring(0, index);
412+
409413
JSONArray transferredArray = new JSONArray();
410414

411415
Object value;
416+
boolean isContainer = true;
417+
boolean isFirst = true;
412418
for (int i = 0; i < responseArray.size(); i++) {
413419
value = responseArray.get(i);
414420
if (value instanceof JSONArray) {//转化JSONArray内部的APIJSON Array
415-
transferredArray.add(format((JSONArray) value));
421+
transferredArray.add(format(null, (JSONArray) value));
416422
} else if (value instanceof JSONObject) {//JSONObject,往下一级提取
417-
transferredArray.add(format((JSONObject) value));
423+
//判断是否需要提取child
424+
if (isFirst && isTableKey(className) && ((JSONObject) value).containsKey(className)) {
425+
isContainer = false;
426+
}
427+
//直接添加child 或 添加提取出的child
428+
transferredArray.add(format(isContainer ? (JSONObject)value : ((JSONObject)value).getJSONObject(className) ));
429+
isFirst = false;
418430
} else {//其它Object,直接填充
419431
transferredArray.add(responseArray.get(i));
420432
}
421433
}
422434

423-
Log.i(TAG, "format return transferredArray = " + JSON.toJSONString(transferredArray));
435+
//太长查看不方便,不如debug Log.i(TAG, "format return transferredArray = " + JSON.toJSONString(transferredArray));
424436
return transferredArray;
425437
}
426438

@@ -430,7 +442,7 @@ public static JSONArray format(final JSONArray responseArray) {
430442
*/
431443
public static String replaceArray(String key) {
432444
if (isArrayKey(key)) {
433-
key = getArrayKey(key.substring(0, key.indexOf(KEY_ARRAY)));
445+
key = getArrayKey(key.substring(0, key.lastIndexOf(KEY_ARRAY)));
434446
}
435447
return getSimpleName(key);
436448
}
@@ -450,7 +462,7 @@ public static String getArrayKey(String key) {
450462
}
451463
return key + "List";
452464
}
453-
465+
454466
/**获取简单名称
455467
* @param fullName name 或 name:alias
456468
* @return name > name; name:alias > alias
@@ -463,6 +475,6 @@ public static String getSimpleName(String fullName) {
463475
}
464476
return fullName;
465477
}
466-
478+
467479

468480
}

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/StringUtil.java

+77-40
Original file line numberDiff line numberDiff line change
@@ -453,46 +453,6 @@ public static boolean isPath(String path) {
453453
&& path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false;
454454
}
455455

456-
/**分割路径
457-
* @param path
458-
* @return
459-
*/
460-
public static String[] splitPath(String path) {
461-
if (StringUtil.isNotEmpty(path, true) == false) {
462-
return null;
463-
}
464-
return isPath(path) ? split(path, SEPARATOR) : new String[] {path};
465-
}
466-
/**将s分割成String[]
467-
* @param s
468-
* @return
469-
*/
470-
public static String[] split(String s) {
471-
return split(s, null);
472-
}
473-
/**将s用split分割成String[]
474-
* @param s
475-
* @param split
476-
* @return
477-
*/
478-
public static String[] split(String s, String split) {
479-
s = getString(s);
480-
if (s.isEmpty()) {
481-
return null;
482-
}
483-
if (isNotEmpty(split, false) == false) {
484-
split = ",";
485-
}
486-
while (s.startsWith(split)) {
487-
s = s.substring(split.length());
488-
}
489-
while (s.endsWith(split)) {
490-
s = s.substring(0, s.length() - split.length());
491-
}
492-
return s.contains(split) ? s.split(split) : new String[]{s};
493-
}
494-
495-
496456
/**判断字符类型是否是路径
497457
* @param path
498458
* @return
@@ -716,6 +676,83 @@ public static String getPrice(double price, int formatType) {
716676
}
717677
}
718678

679+
680+
/**分割路径
681+
* @param path
682+
* @return
683+
*/
684+
public static String[] splitPath(String path) {
685+
if (StringUtil.isNotEmpty(path, true) == false) {
686+
return null;
687+
}
688+
return isPath(path) ? split(path, SEPARATOR) : new String[] {path};
689+
}
690+
/**将s分割成String[]
691+
* @param s
692+
* @return
693+
*/
694+
public static String[] split(String s) {
695+
return split(s, null);
696+
}
697+
/**将s用split分割成String[]
698+
* @param s
699+
* @param split
700+
* @return
701+
*/
702+
public static String[] split(String s, String split) {
703+
s = getString(s);
704+
if (s.isEmpty()) {
705+
return null;
706+
}
707+
if (isNotEmpty(split, false) == false) {
708+
split = ",";
709+
}
710+
while (s.startsWith(split)) {
711+
s = s.substring(split.length());
712+
}
713+
while (s.endsWith(split)) {
714+
s = s.substring(0, s.length() - split.length());
715+
}
716+
return s.contains(split) ? s.split(split) : new String[]{s};
717+
}
718+
719+
/**
720+
* @param key
721+
* @param suffix
722+
* @return key + suffix,第一个字母小写
723+
*/
724+
public static String addSuffix(String key, String suffix) {
725+
key = StringUtil.getNoBlankString(key);
726+
if (key.isEmpty()) {
727+
return firstCase(suffix);
728+
}
729+
730+
return firstCase(key) + firstCase(suffix, true);
731+
}
732+
/**
733+
* @param key
734+
*/
735+
public static String firstCase(String key) {
736+
return firstCase(key, false);
737+
}
738+
/**
739+
* @param key
740+
* @param upper
741+
* @return
742+
*/
743+
public static String firstCase(String key, boolean upper) {
744+
key = StringUtil.getString(key);
745+
if (key.isEmpty()) {
746+
return "";
747+
}
748+
749+
String first = key.substring(0, 1);
750+
key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length());
751+
752+
return key;
753+
}
754+
755+
719756
//校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
720757

721758
}

0 commit comments

Comments
 (0)