@@ -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}
0 commit comments