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"와 같은 문자열 유형이면 원활하게 변환이 가능하고 "abc123"과 같은 문자열 유형이면 오류가 보고됩니다. 물론 필드가 null이면 예외가 발생합니다. .