@@ -46,10 +46,10 @@ public JSONResponse(JSONObject object) { |
46 | 46 | } |
47 | 47 |
|
48 | 48 | //状态信息,非GET请求获得的信息<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
49 | | - |
| 49 | + |
50 | 50 | public static final int STATUS_SUCCEED = 200; |
51 | | - |
52 | | - |
| 51 | + |
| 52 | + |
53 | 53 | public static final String KEY_ID = "id"; |
54 | 54 | public static final String KEY_STATUS = "status"; |
55 | 55 | public static final String KEY_COUNT = "count"; |
@@ -91,7 +91,7 @@ public int getTotal() { |
91 | 91 | public String getMessage() { |
92 | 92 | return getString(KEY_MESSAGE); |
93 | 93 | } |
94 | | - |
| 94 | + |
95 | 95 | /**是否成功 |
96 | 96 | * @return |
97 | 97 | */ |
@@ -112,7 +112,7 @@ public static boolean isSucceed(int status) { |
112 | 112 | public static boolean isSucceed(JSONResponse response) { |
113 | 113 | return response != null && response.isSucceed(); |
114 | 114 | } |
115 | | - |
| 115 | + |
116 | 116 | /**校验服务端是否存在table |
117 | 117 | * @return |
118 | 118 | */ |
@@ -296,10 +296,10 @@ public static JSONArray getArray(JSONObject object, String key, String className |
296 | 296 | public JSONArray toArray(String className) { |
297 | 297 | return toArray(this, className); |
298 | 298 | } |
299 | | - /** |
| 299 | + /**{0:{Table:{}}, 1:{Table:{}}...} 转化为 [{Table:{}}, {Table:{}}] |
300 | 300 | * array.set(index, isContainer ? value : value.getJSONObject(className)); |
301 | 301 | * @param arrayObject |
302 | | - * @param className className.isEmpty() == false && value.containsKey(className) >> isContainer = true; |
| 302 | + * @param className className.equals(Table) ? {Table:{Content}} => {Content} |
303 | 303 | * @return |
304 | 304 | */ |
305 | 305 | public static JSONArray toArray(JSONObject arrayObject, String className) { |
@@ -334,7 +334,7 @@ public static JSONArray toArray(JSONObject arrayObject, String className) { |
334 | 334 | if (value != null) { |
335 | 335 | try { |
336 | 336 | index = Integer.valueOf(0 + key); |
337 | | - if (isFirst && className.isEmpty() == false && value.containsKey(className)) {// 判断是否需要提取table |
| 337 | + if (isFirst && isTableKey(className) && value.containsKey(className)) {// 判断是否需要提取table |
338 | 338 | isContainer = false; |
339 | 339 | } |
340 | 340 | array.set(index, isContainer ? value : value.getJSONObject(className)); |
@@ -349,19 +349,20 @@ public static JSONArray toArray(JSONObject arrayObject, String className) { |
349 | 349 |
|
350 | 350 |
|
351 | 351 |
|
352 | | - // /**将Table[]:{0:{Table:{}}, 1:{Table:{}}...} 转化为 tableList:[{}, {}] |
| 352 | + // /** |
353 | 353 | // * @return |
354 | 354 | // */ |
355 | 355 | // public JSONObject format() { |
356 | 356 | // return format(this); |
357 | 357 | // } |
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} |
359 | 360 | * @param target |
360 | 361 | * @param response |
361 | 362 | * @return |
362 | 363 | */ |
363 | 364 | 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)); |
365 | 366 | if (response == null || response.isEmpty()) { |
366 | 367 | Log.i(TAG, "format response == null || response.isEmpty() >> return response;"); |
367 | 368 | return response; |
@@ -370,19 +371,19 @@ public static JSONObject format(final JSONObject response) { |
370 | 371 |
|
371 | 372 | Set<String> set = response.keySet(); |
372 | 373 | if (set != null) { |
373 | | - |
| 374 | + |
374 | 375 | Object value; |
375 | 376 | String arrayKey; |
376 | 377 | for (String key : set) { |
377 | 378 | value = response.get(key); |
378 | | - |
| 379 | + |
379 | 380 | if (value instanceof JSONArray) {//转化JSONArray内部的APIJSON Array |
380 | | - transferredObject.put(getSimpleName(key), format((JSONArray) value)); |
| 381 | + transferredObject.put(replaceArray(key), format(key, (JSONArray) value)); |
381 | 382 | } else if (value instanceof JSONObject) {//APIJSON Array转为常规JSONArray |
382 | 383 | if (isArrayKey(key)) {//APIJSON Array转为常规JSONArray |
383 | | - arrayKey = key.substring(0, key.indexOf(KEY_ARRAY)); |
| 384 | + arrayKey = key.substring(0, key.lastIndexOf(KEY_ARRAY)); |
384 | 385 | transferredObject.put(getArrayKey(getSimpleName(arrayKey)) |
385 | | - , format(toArray((JSONObject) value, arrayKey)));//需要将name:alias传至toArray |
| 386 | + , format(key, toArray((JSONObject) value, arrayKey)));//需要将name:alias传至toArray |
386 | 387 | } else {//常规JSONObject,往下一级提取 |
387 | 388 | transferredObject.put(getSimpleName(key), format((JSONObject) value)); |
388 | 389 | } |
@@ -392,35 +393,46 @@ public static JSONObject format(final JSONObject response) { |
392 | 393 | } |
393 | 394 | } |
394 | 395 |
|
395 | | - Log.i(TAG, "format return transferredObject = " + JSON.toJSONString(transferredObject)); |
| 396 | + //太长查看不方便,不如debug Log.i(TAG, "format return transferredObject = " + JSON.toJSONString(transferredObject)); |
396 | 397 | return transferredObject; |
397 | 398 | } |
398 | | - |
| 399 | + |
399 | 400 | /** |
400 | 401 | * @param responseArray |
401 | 402 | * @return |
402 | 403 | */ |
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)); |
405 | 406 | if (responseArray == null || responseArray.isEmpty()) { |
406 | 407 | Log.i(TAG, "format responseArray == null || responseArray.isEmpty() >> return response;"); |
407 | 408 | return responseArray; |
408 | 409 | } |
| 410 | + int index = name == null ? -1 : name.lastIndexOf(KEY_ARRAY); |
| 411 | + String className = index < 0 ? "" : name.substring(0, index); |
| 412 | + |
409 | 413 | JSONArray transferredArray = new JSONArray(); |
410 | 414 |
|
411 | 415 | Object value; |
| 416 | + boolean isContainer = true; |
| 417 | + boolean isFirst = true; |
412 | 418 | for (int i = 0; i < responseArray.size(); i++) { |
413 | 419 | value = responseArray.get(i); |
414 | 420 | if (value instanceof JSONArray) {//转化JSONArray内部的APIJSON Array |
415 | | - transferredArray.add(format((JSONArray) value)); |
| 421 | + transferredArray.add(format(null, (JSONArray) value)); |
416 | 422 | } 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; |
418 | 430 | } else {//其它Object,直接填充 |
419 | 431 | transferredArray.add(responseArray.get(i)); |
420 | 432 | } |
421 | 433 | } |
422 | 434 |
|
423 | | - Log.i(TAG, "format return transferredArray = " + JSON.toJSONString(transferredArray)); |
| 435 | + //太长查看不方便,不如debug Log.i(TAG, "format return transferredArray = " + JSON.toJSONString(transferredArray)); |
424 | 436 | return transferredArray; |
425 | 437 | } |
426 | 438 |
|
@@ -430,7 +442,7 @@ public static JSONArray format(final JSONArray responseArray) { |
430 | 442 | */ |
431 | 443 | public static String replaceArray(String key) { |
432 | 444 | if (isArrayKey(key)) { |
433 | | - key = getArrayKey(key.substring(0, key.indexOf(KEY_ARRAY))); |
| 445 | + key = getArrayKey(key.substring(0, key.lastIndexOf(KEY_ARRAY))); |
434 | 446 | } |
435 | 447 | return getSimpleName(key); |
436 | 448 | } |
@@ -450,7 +462,7 @@ public static String getArrayKey(String key) { |
450 | 462 | } |
451 | 463 | return key + "List"; |
452 | 464 | } |
453 | | - |
| 465 | + |
454 | 466 | /**获取简单名称 |
455 | 467 | * @param fullName name 或 name:alias |
456 | 468 | * @return name > name; name:alias > alias |
@@ -463,6 +475,6 @@ public static String getSimpleName(String fullName) { |
463 | 475 | } |
464 | 476 | return fullName; |
465 | 477 | } |
466 | | - |
| 478 | + |
467 | 479 |
|
468 | 480 | } |