I have a function which saves Android data in sqlite
but I have to convert the String
data to an Integer
.
Whenever the S
Simply use the built-in method JSONObject#getInt(String), it will automatically convert the value to an int
by calling behind the scene Integer.parseInt(String)
if it is a String
or by calling Number#intValue()
if it is a Number
. To avoid an exception when your key is not available, simply check first if your JSONObject
instance has your key using JSONObject#has(String), this is enough to be safe because a key cannot have a null
value, either it exists with a non null
value or it doesn't exist.
JSONObject jObj = jsonarray.getJSONObject(i);
int block_id = jObj.has("block_id") ? jObj.getInt("block_id") : 0;