String 和int 之间的强制转换:
public String getString( String key ) {
verifyIsNull();
Object o = get( key );
if( o != null ){
return o.toString();//非空强转的过程
}
throw new JSONException( "JSONObject[" + JSONUtils.quote( key ) + "] not found." );
}
public int getInt(String key) {
verifyIsNull();
Object o = get(key);
if (o != null) {
return o instanceof Number ? ((Number) o).intValue() : (int) getDouble(key);
}
throw new JSONException("JSONObject[" + JSONUtils.quote(key) + "] is not a number.");
}
可以看出倘若字段不是int类型就会报错。但若字段为“1234"这种string类型的话也可以顺利转换;若为”abc123“这种就会报错,当然,字段为null的话也会抛出异常