Skip to content

Commit

Permalink
Server:同步eclipse版至idea版
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyLemon committed Apr 29, 2017
1 parent 610d60e commit e4f988a
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class JSON {
* @return
*/
public static boolean isJsonCorrect(String s) {
// Log.i(TAG, "isJsonCorrect <<<< " + s + " >>>>>>>");
//太长 Log.i(TAG, "isJsonCorrect <<<< " + s + " >>>>>>>");
if (s == null
// || s.equals("[]")
// || s.equals("{}")
Expand Down Expand Up @@ -222,7 +222,7 @@ public static boolean isJSONObject(Object obj) {
Log.e(TAG, "isJSONObject catch \n" + e.getMessage());
}
}

return false;
}
/**判断是否为JSONArray
Expand All @@ -241,7 +241,7 @@ public static boolean isJSONArray(Object obj) {
Log.e(TAG, "isJSONArray catch \n" + e.getMessage());
}
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public JSONResponse(JSONObject object) {
}

//状态信息,非GET请求获得的信息<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

public static final int STATUS_SUCCEED = 200;


public static final String KEY_ID = "id";
public static final String KEY_STATUS = "status";
public static final String KEY_COUNT = "count";
Expand Down Expand Up @@ -91,7 +91,7 @@ public int getTotal() {
public String getMessage() {
return getString(KEY_MESSAGE);
}

/**是否成功
* @return
*/
Expand All @@ -112,7 +112,7 @@ public static boolean isSucceed(int status) {
public static boolean isSucceed(JSONResponse response) {
return response != null && response.isSucceed();
}

/**校验服务端是否存在table
* @return
*/
Expand Down Expand Up @@ -296,10 +296,10 @@ public static JSONArray getArray(JSONObject object, String key, String className
public JSONArray toArray(String className) {
return toArray(this, className);
}
/**
/**{0:{Table:{}}, 1:{Table:{}}...} 转化为 [{Table:{}}, {Table:{}}]
* array.set(index, isContainer ? value : value.getJSONObject(className));
* @param arrayObject
* @param className className.isEmpty() == false && value.containsKey(className) >> isContainer = true;
* @param className className.equals(Table) ? {Table:{Content}} => {Content}
* @return
*/
public static JSONArray toArray(JSONObject arrayObject, String className) {
Expand Down Expand Up @@ -334,7 +334,7 @@ public static JSONArray toArray(JSONObject arrayObject, String className) {
if (value != null) {
try {
index = Integer.valueOf(0 + key);
if (isFirst && className.isEmpty() == false && value.containsKey(className)) {// 判断是否需要提取table
if (isFirst && isTableKey(className) && value.containsKey(className)) {// 判断是否需要提取table
isContainer = false;
}
array.set(index, isContainer ? value : value.getJSONObject(className));
Expand All @@ -349,19 +349,20 @@ public static JSONArray toArray(JSONObject arrayObject, String className) {



// /**将Table[]:{0:{Table:{}}, 1:{Table:{}}...} 转化为 tableList:[{}, {}]
// /**
// * @return
// */
// public JSONObject format() {
// return format(this);
// }
/**将Table[]:{0:{Table:{}}, 1:{Table:{}}...} 转化为 tableList:[{}, {}]
/**将Item[]:[{Table:{}}, {Table:{}}...] 或 Item[]:{0:{Table:{}}, 1:{Table:{}}...}
* 转化为 itemList:[{Table:{}}, {Table:{}}],如果 Item.equals(Table),则将 {Table:{Content}} 转化为 {Content}
* @param target
* @param response
* @return
*/
public static JSONObject format(final JSONObject response) {
Log.i(TAG, "format response = \n" + JSON.toJSONString(response));
//太长查看不方便,不如debug Log.i(TAG, "format response = \n" + JSON.toJSONString(response));
if (response == null || response.isEmpty()) {
Log.i(TAG, "format response == null || response.isEmpty() >> return response;");
return response;
Expand All @@ -370,19 +371,19 @@ public static JSONObject format(final JSONObject response) {

Set<String> set = response.keySet();
if (set != null) {

Object value;
String arrayKey;
for (String key : set) {
value = response.get(key);

if (value instanceof JSONArray) {//转化JSONArray内部的APIJSON Array
transferredObject.put(getSimpleName(key), format((JSONArray) value));
transferredObject.put(replaceArray(key), format(key, (JSONArray) value));
} else if (value instanceof JSONObject) {//APIJSON Array转为常规JSONArray
if (isArrayKey(key)) {//APIJSON Array转为常规JSONArray
arrayKey = key.substring(0, key.indexOf(KEY_ARRAY));
arrayKey = key.substring(0, key.lastIndexOf(KEY_ARRAY));
transferredObject.put(getArrayKey(getSimpleName(arrayKey))
, format(toArray((JSONObject) value, arrayKey)));//需要将name:alias传至toArray
, format(key, toArray((JSONObject) value, arrayKey)));//需要将name:alias传至toArray
} else {//常规JSONObject,往下一级提取
transferredObject.put(getSimpleName(key), format((JSONObject) value));
}
Expand All @@ -392,35 +393,46 @@ public static JSONObject format(final JSONObject response) {
}
}

Log.i(TAG, "format return transferredObject = " + JSON.toJSONString(transferredObject));
//太长查看不方便,不如debug Log.i(TAG, "format return transferredObject = " + JSON.toJSONString(transferredObject));
return transferredObject;
}

/**
* @param responseArray
* @return
*/
public static JSONArray format(final JSONArray responseArray) {
Log.i(TAG, "format responseArray = \n" + JSON.toJSONString(responseArray));
public static JSONArray format(String name, final JSONArray responseArray) {
//太长查看不方便,不如debug Log.i(TAG, "format responseArray = \n" + JSON.toJSONString(responseArray));
if (responseArray == null || responseArray.isEmpty()) {
Log.i(TAG, "format responseArray == null || responseArray.isEmpty() >> return response;");
return responseArray;
}
int index = name == null ? -1 : name.lastIndexOf(KEY_ARRAY);
String className = index < 0 ? "" : name.substring(0, index);

JSONArray transferredArray = new JSONArray();

Object value;
boolean isContainer = true;
boolean isFirst = true;
for (int i = 0; i < responseArray.size(); i++) {
value = responseArray.get(i);
if (value instanceof JSONArray) {//转化JSONArray内部的APIJSON Array
transferredArray.add(format((JSONArray) value));
transferredArray.add(format(null, (JSONArray) value));
} else if (value instanceof JSONObject) {//JSONObject,往下一级提取
transferredArray.add(format((JSONObject) value));
//判断是否需要提取child
if (isFirst && isTableKey(className) && ((JSONObject) value).containsKey(className)) {
isContainer = false;
}
//直接添加child 或 添加提取出的child
transferredArray.add(format(isContainer ? (JSONObject)value : ((JSONObject)value).getJSONObject(className) ));
isFirst = false;
} else {//其它Object,直接填充
transferredArray.add(responseArray.get(i));
}
}

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

Expand All @@ -430,7 +442,7 @@ public static JSONArray format(final JSONArray responseArray) {
*/
public static String replaceArray(String key) {
if (isArrayKey(key)) {
key = getArrayKey(key.substring(0, key.indexOf(KEY_ARRAY)));
key = getArrayKey(key.substring(0, key.lastIndexOf(KEY_ARRAY)));
}
return getSimpleName(key);
}
Expand All @@ -450,7 +462,7 @@ public static String getArrayKey(String key) {
}
return key + "List";
}

/**获取简单名称
* @param fullName name 或 name:alias
* @return name > name; name:alias > alias
Expand All @@ -463,6 +475,6 @@ public static String getSimpleName(String fullName) {
}
return fullName;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -453,46 +453,6 @@ public static boolean isPath(String path) {
&& path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false;
}

/**分割路径
* @param path
* @return
*/
public static String[] splitPath(String path) {
if (StringUtil.isNotEmpty(path, true) == false) {
return null;
}
return isPath(path) ? split(path, SEPARATOR) : new String[] {path};
}
/**将s分割成String[]
* @param s
* @return
*/
public static String[] split(String s) {
return split(s, null);
}
/**将s用split分割成String[]
* @param s
* @param split
* @return
*/
public static String[] split(String s, String split) {
s = getString(s);
if (s.isEmpty()) {
return null;
}
if (isNotEmpty(split, false) == false) {
split = ",";
}
while (s.startsWith(split)) {
s = s.substring(split.length());
}
while (s.endsWith(split)) {
s = s.substring(0, s.length() - split.length());
}
return s.contains(split) ? s.split(split) : new String[]{s};
}


/**判断字符类型是否是路径
* @param path
* @return
Expand Down Expand Up @@ -716,6 +676,83 @@ public static String getPrice(double price, int formatType) {
}
}


/**分割路径
* @param path
* @return
*/
public static String[] splitPath(String path) {
if (StringUtil.isNotEmpty(path, true) == false) {
return null;
}
return isPath(path) ? split(path, SEPARATOR) : new String[] {path};
}
/**将s分割成String[]
* @param s
* @return
*/
public static String[] split(String s) {
return split(s, null);
}
/**将s用split分割成String[]
* @param s
* @param split
* @return
*/
public static String[] split(String s, String split) {
s = getString(s);
if (s.isEmpty()) {
return null;
}
if (isNotEmpty(split, false) == false) {
split = ",";
}
while (s.startsWith(split)) {
s = s.substring(split.length());
}
while (s.endsWith(split)) {
s = s.substring(0, s.length() - split.length());
}
return s.contains(split) ? s.split(split) : new String[]{s};
}

/**
* @param key
* @param suffix
* @return key + suffix,第一个字母小写
*/
public static String addSuffix(String key, String suffix) {
key = StringUtil.getNoBlankString(key);
if (key.isEmpty()) {
return firstCase(suffix);
}

return firstCase(key) + firstCase(suffix, true);
}
/**
* @param key
*/
public static String firstCase(String key) {
return firstCase(key, false);
}
/**
* @param key
* @param upper
* @return
*/
public static String firstCase(String key, boolean upper) {
key = StringUtil.getString(key);
if (key.isEmpty()) {
return "";
}

String first = key.substring(0, 1);
key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length());

return key;
}


//校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

}
Loading

0 comments on commit e4f988a

Please sign in to comment.