import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.HashMap;
/**
* 数值获取工具类
*/
public class ObtainValueUtil {
/**
* 根据传入的JSON字符串进行解析成一个JSON对象
*
* @param json 传入一个JSON字符串
* @return
*/
public static JSONObject getJSONObject(String json) {
try {
if (!TextUtils.isEmpty(json)) {
return new JSONObject(json);
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
/**
* 根据传入的JSON字符串和键名进行解析成一个JSON对象
*
* @param json 传入一个JSON字符串
* @param key 键名
* @return
*/
public static JSONObject getJSONObject(JSONObject json, String key) {
try {
if (json != null && !TextUtils.isEmpty(key)) {
return json.isNull(key) ? null : json.getJSONObject(key);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 根据传入的JSON数组和数组下标获取一个JSON对象
*
* @param array 传入一个JSON数组
* @param index 传入一个数组下标
* @return
*/
public static JSONObject getJSONObject(JSONArray array, int index) {
JSONObject json = null;
try {
if (array != null && index >= 0 && index < array.length()) {
json = array.isNull(index) ? null : array.getJSONObject(index);
}
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
/**
* 根据传入的JSON字符串解析成JSON数组对象
*
* @param json 传入一个JSON字符串
* @return
*/
public static JSONArray getJSONArray(String json) {
try {
if (!TextUtils.isEmpty(json)) {
return new JSONArray(json);
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
/**
* 根据传入的JSON对象和键名获取一个JSON数组
*
* @param json 传入的JSON对象
* @param key 传入的键名
* @return
*/
public static JSONArray getJSONArray(JSONObject json, String key) {
JSONArray array = null;
try {
if (json != null && !TextUtils.isEmpty(key)) {
array = json.isNull(key) ? null : json.getJSONArray(key);
}
} catch (Exception e) {
e.printStackTrace();
}
return array;
}
/**
* 根据传入的JSON数组和数组下标获取一个JSON数组
*
* @param array 传入一个JSON数组
* @param index 传入数组下标
* @return
*/
public static JSONArray getJSONArray(JSONArray array, int index) {
JSONArray jsonArray = null;
try {
if (array != null && index >= 0 && index < array.length()) {
jsonArray = array.isNull(index) ? null : array.getJSONArray(index);
}
} catch (Exception e) {
e.printStackTrace();
}
return jsonArray;
}
/**
* 根据传入的JSON数组和数组下标获取一个字符串
*
* @param array 传入一个JSON数组
* @param index 传入一个数组下标
* @return
*/
public static String getJSONValue(JSONArray array, int index) {
String text = "";
try {
if (array != null && index >= 0 && index < array.length()) {
text = array.isNull(index) ? "" : array.getString(index);
}
} catch (Exception e) {
e.printStackTrace();
}
return text;
}
/**
* 根据传入的JSON对象和键名获取一个字符串
*
* @param json 传入一个JSON对象
* @param key 传入一个键名
* @return
*/
public static String getJSONValue(JSONObject json, String key) {
String text = "";
try {
if (json != null && !TextUtils.isEmpty(key)) {
text = json.isNull(key) ? "" : json.getString(key);
}
} catch (Exception e) {
e.printStackTrace();
}
return text;
}
/**
* 根据传入的对象和键名,做非空判断和类型判断,返回一个字符串,默认返回空字符串
*
* @param object 传入的对象
* @param key 传入的键名
* @return
*/
public static String getString(Object object, String key) {
String value = "";
try {
if (object != null && !TextUtils.isEmpty(key)) {
if (object instanceof Intent) {
value = ((Intent) object).getStringExtra(key);
} else if (object instanceof Bundle) {
Bundle bundle = (Bundle) object;
value = bundle.containsKey(key) ? bundle.getString(key) : value;
} else if (object instanceof HashMap) {
HashMap hashMap = ((HashMap) object);
value = hashMap.containsKey(key) ? String.valueOf(hashMap.get(key)) : value;
} else if (object instanceof ContentValues) {
ContentValues contentValues = ((ContentValues) object);
value = contentValues.containsKey(key) ? contentValues.getAsString(key) : value;
} else if (object instanceof JSONObject) {
JSONObject jsonObject = ((JSONObject) object);
value = jsonObject.isNull(key) ? value : jsonObject.getString(key);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
/**
* 根据传入的对象和键名,做非空判断和类型判断,返回一个字符串,默认返回传入的默认值
*
* @param object 传入的对象
* @param key 传入的键名
* @param defaultValue 传入的默认值
* @return
*/
public static String getString(Object object, String key, String defaultValue) {
String value = defaultValue;
try {
if (object != null && !TextUtils.isEmpty(key)) {
if (object instanceof Intent) {
value = ((Intent) object).getStringExtra(key);
if (TextUtils.isEmpty(value)) {
value = defaultValue;
}
} else if (object instanceof Bundle) {
Bundle bundle = (Bundle) object;
value = bundle.containsKey(key) ? bundle.getString(key) : defaultValue;
} else if (object instanceof HashMap) {
HashMap hashMap = ((HashMap) object);
value = hashMap.containsKey(key) ? String.valueOf(hashMap.get(key)) : defaultValue;
} else if (object instanceof ContentValues) {
ContentValues contentValues = ((ContentValues) object);
value = contentValues.containsKey(key) ? contentValues.getAsString(key) : defaultValue;
} else if (object instanceof JSONObject) {
JSONObject jsonObject = ((JSONObject) object);
value = jsonObject.isNull(key) ? defaultValue : jsonObject.getString(key);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
/**
* 根据传入的对象和键名,做非空判断和类型判断,返回一个布尔值,默认返回false
*
* @param object 传入的对象
* @param key 传入的键名
* @return
*/
public static boolean getBoolean(Object object, String key) {
boolean value = false;
try {
if (object != null && !TextUtils.isEmpty(key)) {
if (object instanceof Intent) {
value = ((Intent) object).getBooleanExtra(key, false);
} else if (object instanceof Bundle) {
Bundle bundle = (Bundle) object;
value = bundle.containsKey(key) ? bundle.getBoolean(key) : false;
} else if (object instanceof HashMap) {
HashMap hashMap = ((HashMap) object);
value = (hashMap.containsKey(key) && hashMap.get(key) instanceof Boolean) ? (Boolean) hashMap.get(key) : false;
} else if (object instanceof ContentValues) {
ContentValues contentValues = ((ContentValues) object);
value = contentValues.containsKey(key) ? contentValues.getAsBoolean(key) : false;
} else if (object instanceof JSONObject) {
JSONObject jsonObject = ((JSONObject) object);
value = jsonObject.isNull(key) ? false : jsonObject.getBoolean(key);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
/**
* 根据传入的对象和键名,做非空判断和类型判断,返回一个布尔值,默认返回传入的默认值
*
* @param object 传入的对象
* @param key 传入的键名
* @param defaultValue 传入的默认值
* @return
*/
public static boolean getBoolean(Object object, String key, boolean defaultValue) {
boolean value = defaultValue;
try {
if (object != null && !TextUtils.isEmpty(key)) {
if (object instanceof Intent) {
value = ((Intent) object).getBooleanExtra(key, defaultValue);
} else if (object instanceof Bundle) {
Bundle bundle = (Bundle) object;
value = bundle.containsKey(key) ? bundle.getBoolean(key) : defaultValue;
} else if (object instanceof HashMap) {
HashMap hashMap = ((HashMap) object);
value = (hashMap.containsKey(key) && hashMap.get(key) instanceof Boolean) ? (Boolean) hashMap.get(key) : defaultValue;
} else if (object instanceof ContentValues) {
ContentValues contentValues = ((ContentValues) object);
value = contentValues.containsKey(key) ? contentValues.getAsBoolean(key) : defaultValue;
} else if (object instanceof JSONObject) {
JSONObject jsonObject = ((JSONObject) object);
value = jsonObject.isNull(key) ? defaultValue : jsonObject.getBoolean(key);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
/**
* 根据传入的对象和键名,做非空判断和类型判断,返回一个整型,默认返回-1
*
* @param object 传入的对象
* @param key 传入的键名
* @return
*/
public static int getInt(Object object, String key) {
int value = -1;
try {
if (object != null && !TextUtils.isEmpty(key)) {
if (object instanceof Intent) {
value = ((Intent) object).getIntExtra(key, value);
} else if (object instanceof Bundle) {
Bundle bundle = (Bundle) object;
value = bundle.containsKey(key) ? bundle.getInt(key) : value;
} else if (object instanceof HashMap) {
HashMap hashMap = ((HashMap) object);
value = (hashMap.containsKey(key) && hashMap.get(key) instanceof Integer) ? (Integer) hashMap.get(key) : value;
} else if (object instanceof ContentValues) {
ContentValues contentValues = ((ContentValues) object);
value = contentValues.containsKey(key) ? contentValues.getAsInteger(key) : value;
} else if (object instanceof JSONObject) {
JSONObject jsonObject = ((JSONObject) object);
value = jsonObject.isNull(key) ? value : jsonObject.getInt(key);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
/**
* 根据传入的对象和键名,做非空判断和类型判断,返回一个整型,默认返回传入的默认值
*
* @param object 传入的对象
* @param key 传入的键名
* @param defaultValue 传入的默认值
* @return
*/
public static int getInt(Object object, String key, int defaultValue) {
int value = defaultValue;
try {
if (object != null && !TextUtils.isEmpty(key)) {
if (object instanceof Intent) {
value = ((Intent) object).getIntExtra(key, defaultValue);
} else if (object instanceof Bundle) {
Bundle bundle = (Bundle) object;
value = bundle.containsKey(key) ? bundle.getInt(key) : defaultValue;
} else if (object instanceof HashMap) {
HashMap hashMap = ((HashMap) object);
value = (hashMap.containsKey(key) && hashMap.get(key) instanceof Integer) ? (Integer) hashMap.get(key) : defaultValue;
} else if (object instanceof ContentValues) {
ContentValues contentValues = ((ContentValues) object);
value = contentValues.containsKey(key) ? contentValues.getAsInteger(key) : defaultValue;
} else if (object instanceof JSONObject) {
JSONObject jsonObject = ((JSONObject) object);
value = jsonObject.isNull(key) ? defaultValue : jsonObject.getInt(key);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
}