型別安全轉換的工具類

2021-07-11 10:39:41 字數 1226 閱讀 3993

有時候我們發現,解析下來的資料有時候會因為後台沒有處理好,會造成空指標的現象,而導致程式崩潰.

這裡是因為我們對 object 物件沒有進行很好的安全轉換.

正常來說:我們拉去的解析資料

int result=integer.valueof(obj.getstring(「result」));

這時候一旦 obj(object)為空,那麼程式就會崩掉.

所以這裡我們要建立乙個輔助的工具類:包括型別安全轉換函式的工具類.

converttype(型別安全轉換的工具類)

/**

* 型別安全轉換函式

* *@anthor superd

* create at 下午9:29

*/public

class

converttype

try catch (exception e) catch (exception e1) }}

/*** object-->string

**@param value

*@param defaultvalue

*@return

*/public

static string converttostring(object value, string defaultvalue)

return value.tostring();

}/**

* object-->long

**@param value

*@param defaultvalue

*@return

*/public

static

long

converttolong(object value, long defaultvalue)

return long.valueof(value.tostring());

}/**

* object-->boolean

*@param value

*@param defaultvalue

*@return

*/public

static

boolean

converttoboolean(object value, boolean defaultvalue)

return boolean.valueof(value.tostring());

}}

型別轉換工具類

做專案的時候自己封裝的工具類,方便查詢,放到這裡 public class convertutil inttostring int轉string author lelonta param i return public static string inttostr int i dubtobigdec...

json工具類,型別轉換

public class jsonutil 物件轉換為json字串 param obj 待轉換物件 return json字串 throws exception public static string tojson object obj throws exception 物件轉換為json字串 p...

C 型別轉換工具類

using system namespace catch exception 轉換為可空整型 資料 public static int?tointornull this object data 轉換為雙精度浮點數 資料 public static double todouble this objec...