android数值获取工具类

 

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;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值