Bây giờ tôi viết một cách phổ biến để lấy dữ liệu JSONObject từ một khóa. Bây giờ tôi phải thay đổi kiểu này thành một phương thức chung?Làm thế nào để thay đổi phương pháp của tôi thành một phương pháp chung?
String a= (String) ObdDeviceTool.getResultData(result, "a", String.class);
Double b= (Double) ObdDeviceTool.getResultData(result, "b", Double.class);
public static Object getJSONObjectData(JSONObject result,String key,Object type){
if (result.containsKey(key)) {
if(type.equals(String.class))
return result.getString(key);
if(type.equals(Double.class))
return result.getDouble(key);
if(type.equals(Long.class))
return result.getLong(key);
if(type.equals(Integer.class))
return result.getInt(key);
}
return null;
}
@ JonnyHenly, tôi đã thử, nhưng tìm thấy nó không thể thay đổi.public static T getJSONObjectData (kết quả JSONObject, String key, loại T) –
flower