[ Web Proxy ]
URL:
Viewing:
https://raw.githubusercontent.com/TextLight1/APIJSON/master/APIJSON-JavaScript/ResponseUtil.js
[Back]
[Original]
/*Copyright 2016 TommyLemon(https://github.com/TommyLemon/APIJSON) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.*/ /**parser for response * @author Lemon * @see #getObject * @see #getList * @use JSONResponse response = new JSONResponse(json); *
User user = response.getObject(User.class);//not a must *
List commentList = response.getList("Comment[]", Comment.class);//not a must */ //GET>> /**key * @param object * @return */ function formatObject(object) { //debug log(TAG, "format object = \n" + JSON.toJSONString(object)); if (object == null || object == '') { log(TAG, "format object == null || object == '' >> return object;"); return object; } let formattedObject = {}; let value; for (let key in object) { value = object[key]; if (value instanceof Array) { // JSONArrayformat formattedObject[replaceArray(key)] = formatArray(value); } else if (value instanceof Object) { // JSONObject formattedObject[getSimpleName(key)] = formatObject(value); } else { // Object formattedObject[getSimpleName(key)] = value; } } //debug log(TAG, "format return formattedObject = " + JSON.toJSONString(formattedObject)); return formattedObject; } /**key * @param array * @return */ function formatArray(array) { //debug log(TAG, "format array = \n" + JSON.toJSONString(array)); if (array == null || array == '') { log(TAG, "format array == null || array == '' >> return array;"); return array; } let formattedArray = []; let value; for (let i = 0; i < array.length; i++) { value = array[i]; if (value instanceof Array) { // JSONArrayformat formattedArray.push(formatArray(value)); } else if (value instanceof Object) { // JSONObject formattedArray.push(formatObject(value)); } else { // Object formattedArray.push(value); } } //debug log(TAG, "format return formattedArray = " + JSON.toJSONString(formattedArray)); return formattedArray; } /**key+KEY_ARRAYkeyList * @param key * @return getSimpleName(isArrayKey(key) ? getArrayKey(...) : key) {@link #getSimpleName(String)} */ function replaceArray(key) { if (isArrayKey(key)) { key = getArrayKey(key.substring(0, key.lastIndexOf('[]'))); } return getSimpleName(key); } /** * @param key => getNoBlankString(key) * @return empty ? "list" : key + "List" */ function getArrayKey(key) { return addSuffix(key, "list"); } /** * @param fullName name name:alias * @return name => name; name:alias => alias */ function getSimpleName(fullName) { //key:alias -> alias; key:alias[] -> alias[] let index = fullName == null ? -1 : fullName.indexOf(":"); if (index >= 0) { fullName = fullName.substring(index + 1); } return fullName; }
Web Proxy Viewer |
New URL
|
Original Page