package com.backfire.doudizhu.common;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class JSONUtil {

	public static JSONObject parse(String json) {
		if (null == json || 0 == json.length()) {
			return null;
		}
		try {
			return new JSONObject(json);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}

	}

	public static JSONObject getJSONObject(JSONObject obj, String name) {
		if (null == obj)
			return null;
		try {
			return obj.getJSONObject(name);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
	public static String getString(JSONObject obj, String name,
			String defaultValue) {
		if (null == obj)
			return defaultValue;
		try {
			String str = obj.getString(name);
			// if ( null == str || 0 == str.length() )
			if (null == str || str.trim().equalsIgnoreCase("null")) {
				return defaultValue;
			}
			return str;
		} catch (JSONException e) {
			e.printStackTrace();
			return defaultValue;
		}
	}

	public static int getInt(JSONObject obj, String name, int defaultValue) {
		if (null == obj)
			return defaultValue;
		try {
			return obj.getInt(name);
		} catch (JSONException e) {
			e.printStackTrace();
			return defaultValue;
		}
	}

	public static long getLong(JSONObject obj, String name, long defaultValue) {
		if (null == obj)
			return defaultValue;
		try {
			return obj.getLong(name);
		} catch (JSONException e) {
			e.printStackTrace();
			return defaultValue;
		}
	}

	public static double getDouble(JSONObject obj, String name,
			double defaultValue) {
		if (null == obj)
			return defaultValue;
		try {
			return obj.getDouble(name);
		} catch (JSONException e) {
			e.printStackTrace();
			return defaultValue;
		}
	}

	public static boolean getBoolean(JSONObject obj, String name,
			boolean defaultValue) {
		if (null == obj)
			return defaultValue;
		try {
			return obj.getBoolean(name);
		} catch (JSONException e) {
			e.printStackTrace();
			return defaultValue;
		}
	}

	public static JSONArray getJSONArray(JSONObject obj, String name) {
		if (null == obj) {
			return null;
		}
		try {
			return obj.getJSONArray(name);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static JSONArray parseArray(String json) {
		try {
			if (null == json || 0 == json.length()) {
				return null;
			}
			return new JSONArray(json);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static int getInt(JSONArray arr, int id, int defaultValue) {
		if (null == arr)
			return defaultValue;
		try {
			return arr.getInt(id);
		} catch (JSONException e) {
			e.printStackTrace();
			return defaultValue;
		}
	}

	public static JSONObject getJSONObject(JSONArray arr, int id) {
		if (null == arr)
			return null;
		try {
			return arr.getJSONObject(id);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String getString(JSONArray arr, int id, String defaultValue) {
		if (null == arr)
			return defaultValue;
		try {
			String str = arr.getString(id);
			if (null == str || str.trim().equalsIgnoreCase("null")) {
				return defaultValue;
			}
			return str;
		} catch (JSONException e) {
			e.printStackTrace();
			return defaultValue;
		}
	}

	public static JSONArray getJSONArrayByJOSNArray(JSONArray arr, int id) {
		if (null == arr)
			return null;
		try {
			return arr.getJSONArray(id);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

}
