package com.cloudcross.ssp.service.impl;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;




public class ObjectBase {

	public JSONArray getJSONArray(JSONObject obj,String key){
		
		JSONArray jsonArray= new JSONArray();
		
	
		try {
	        if (obj.get(key)!=null)		jsonArray=obj.getJSONArray(key);
			
	        
			}catch (JSONException e){
				
				;
				
			}	
		return jsonArray;
		
	}
	public String getJSONObjectString(JSONObject obj,String key,String defaultValue){
		
		String ret=defaultValue;
		
		try {
		 ret=obj.getString(key);
		 
		 if (ret==null||ret.isEmpty()) ret=defaultValue;
		
		}catch (JSONException e){
			
			;
			
		}
		
		return ret;
		
		
	}
	
	
	public String getString(Object obj){
		
		String ret="";
		
		try {
			
		 ret=String.valueOf(obj);
		 
		 if (isNULLStr(ret)) ret="";
		
		}catch (Exception e){
			
			;
			
		}
		
		return ret;
		
		
	}
	
	public static boolean isNULLStr (String input) {

		
		if(input==null){
			return true;
		}
		
		if(StringUtils.isBlank(input)){
			return true;
		}
		if(input.trim().equals("")){
			return true;
		}
		
		if(input.equalsIgnoreCase("null")){
			return true;
		}
		
		return false;
	}

	private boolean isNotNull(String str) {
		if(str != null && !str.equals("")){
			return true;
		}
		return false;
	}
	
	public long getLong(Object obj){
		
		long ret=0;
		
		try {
			
	
			ret=Long.parseLong(String.valueOf(obj));
		 
		
		}catch (Exception e){
			
			//e.printStackTrace();
			
		}
		
		return ret;
		
		
	}
	
	
	public double getDouble(Object obj){
		
		double ret=0;
		
		try {
			
	
			ret=Double.parseDouble(String.valueOf(obj));
		 
		
		}catch (Exception e){
			
			//e.printStackTrace();
			
		}
		
		return ret;
		
		
	}
	
	
	public float getFloat(Object obj){
		
		float ret=0;
		
		try {
			
	
			ret=Float.parseFloat(String.valueOf(obj));
		 
		
		}catch (Exception e){
			
			//e.printStackTrace();
			
		}
		
		return ret;
		
		
	}
	
	
	public String getJSONObjectBoolean(JSONObject obj,String key){
		
		String ret="0";
		
		try {
		 ret=obj.getString(key);
		 
		 if (ret.isEmpty()) {
			 
			 ret="0";
		 
		 }else if ("true".equals(ret)){
			 
			 ret="1";
		 }
			 
				 
		
		}catch (JSONException e){
			
			;
			
		}
		
		return ret;
		
		
	}
	
	public String getJSONObjectLong(JSONObject obj,String key){
		
		String ret="0";
		
		try {
		 ret=obj.getString(key);
		 
		 if (ret.isEmpty()) ret="0";
		
		}catch (JSONException e){
			
			;
			
		}
		
		return ret;
		
		
	}
	
	public double getJSONObjectDouble(JSONObject obj,String key,double defaultValue){
		
		double ret=0;
		
		try {
		 java.text.DecimalFormat myformat=new java.text.DecimalFormat("#0.00");
		 ret=obj.getDouble(key);//装换为double类型
		 ret=Double.parseDouble(myformat.format(ret));//保留2为小数
		
		}catch (Exception e){
			
			;
			
		}
		
		return ret;
		
		
	}
	
	public float getJSONObjectFloat(JSONObject obj,String key,float defaultValue){
		
		float ret=0;
		
		try {
		 ret=Float.parseFloat(obj.getString(key));
		 java.text.DecimalFormat myformat=new java.text.DecimalFormat("#0.00");
		 ret=Float.parseFloat(myformat.format(ret));//装换为double类型
		}catch (Exception e){
			
			;
			
		}
		
		return ret;
		
		
	}
	
	public int getJSONObjectInt(JSONObject obj,String key,int defaultValue){
		
		int ret=0;
		
		try {
		 ret=obj.getInt(key);
		 
		}catch (Exception ex){
			
			//ex.printStackTrace();
			
		}
		
		return ret;
		
		
	}
	
	public long getJSONObjectLong(JSONObject obj,String key,int defaultValue){
		
		long ret=defaultValue;
		
		try {
		 ret=obj.getLong(key);
		
		}catch (Exception e){
			
			ret=defaultValue;
			
		}
		
		return ret;
		
		
	}
	public JSONObject getJSONObject(JSONObject obj,String key){
		
		JSONObject ret=null;
		
		try {
		 ret=obj.getJSONObject(key);
		
		 
		 if (ret.isEmpty()){
			 
			 ret=null;
		 }
		}catch (JSONException e){
			
			;
			
		}
		
		return ret;
		
		
	}
}
