Skip to content

Commit

Permalink
Add getStringCascade getJSONObjectCascade for JSONUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinea committed Aug 12, 2014
1 parent ac78ada commit 31b0e42
Showing 1 changed file with 114 additions and 1 deletion.
115 changes: 114 additions & 1 deletion src/cn/trinea/android/common/util/JSONUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,62 @@ public static String getString(String jsonData, String key, String defaultValue)
}
}

/**
* get String from jsonObject
*
* @param jsonObject
* @param defaultValue
* @param keyArray
* @return <ul>
* <li>if jsonObject is null, return defaultValue</li>
* <li>if keyArray is null or empty, return defaultValue</li>
* <li>get {@link #getJSONObject(JSONObject, String, JSONObject)} by recursion, return it. if anyone is
* null, return directly</li>
* </ul>
*/
public static String getStringCascade(JSONObject jsonObject, String defaultValue, String... keyArray) {
if (jsonObject == null || ArrayUtils.isEmpty(keyArray)) {
return defaultValue;
}

String data = jsonObject.toString();
for (String key : keyArray) {
data = getStringCascade(data, key, defaultValue);
if (data == null) {
return defaultValue;
}
}
return data;
}

/**
* get String from jsonData
*
* @param jsonData
* @param defaultValue
* @param keyArray
* @return <ul>
* <li>if jsonData is null, return defaultValue</li>
* <li>if keyArray is null or empty, return defaultValue</li>
* <li>get {@link #getJSONObject(JSONObject, String, JSONObject)} by recursion, return it. if anyone is
* null, return directly</li>
* </ul>
*/
public static String getStringCascade(String jsonData, String defaultValue, String... keyArray) {
if (StringUtils.isEmpty(jsonData)) {
return defaultValue;
}

String data = jsonData;
for (String key : keyArray) {
data = getString(data, key, defaultValue);
if (data == null) {
return defaultValue;
}
}
return data;
}

/**
* get String array from jsonObject
*
Expand Down Expand Up @@ -474,7 +530,7 @@ public static JSONObject getJSONObject(JSONObject jsonObject, String key, JSONOb
* @param key
* @param defaultValue
* @return <ul>
* <li>if jsonObject is null, return defaultValue</li>
* <li>if jsonData is null, return defaultValue</li>
* <li>if jsonData {@link JSONObject#JSONObject(String)} exception, return defaultValue</li>
* <li>return {@link JSONUtils#getJSONObject(JSONObject, String, JSONObject)}</li>
* </ul>
Expand All @@ -495,6 +551,63 @@ public static JSONObject getJSONObject(String jsonData, String key, JSONObject d
}
}

/**
* get JSONObject from jsonObject
*
* @param jsonObject
* @param defaultValue
* @param keyArray
* @return <ul>
* <li>if jsonObject is null, return defaultValue</li>
* <li>if keyArray is null or empty, return defaultValue</li>
* <li>get {@link #getJSONObject(JSONObject, String, JSONObject)} by recursion, return it. if anyone is
* null, return directly</li>
* </ul>
*/
public static JSONObject getJSONObjectCascade(JSONObject jsonObject, JSONObject defaultValue, String... keyArray) {
if (jsonObject == null || ArrayUtils.isEmpty(keyArray)) {
return defaultValue;
}

JSONObject js = jsonObject;
for (String key : keyArray) {
js = getJSONObject(js, key, defaultValue);
if (js == null) {
return defaultValue;
}
}
return js;
}

/**
* get JSONObject from jsonData
*
* @param jsonData
* @param defaultValue
* @param keyArray
* @return <ul>
* <li>if jsonData is null, return defaultValue</li>
* <li>if keyArray is null or empty, return defaultValue</li>
* <li>get {@link #getJSONObject(JSONObject, String, JSONObject)} by recursion, return it. if anyone is
* null, return directly</li>
* </ul>
*/
public static JSONObject getJSONObjectCascade(String jsonData, JSONObject defaultValue, String... keyArray) {
if (StringUtils.isEmpty(jsonData)) {
return defaultValue;
}

try {
JSONObject jsonObject = new JSONObject(jsonData);
return getJSONObjectCascade(jsonObject, defaultValue, keyArray);
} catch (JSONException e) {
if (isPrintException) {
e.printStackTrace();
}
return defaultValue;
}
}

/**
* get JSONArray from jsonObject
*
Expand Down

0 comments on commit 31b0e42

Please sign in to comment.