如何根据接收的数据更改数据类型

时间:2017-10-13 12:42:35

标签: android json rest

我正在使用REST api获取数据。 JSON行看起来像"temp_c":12,,我就像这样

int temp =  (int) list.get("temp_c");

问题是行可以有int或double类型(如temp_c":12.5,)并且我有错误

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double

如果我使用

double temp =  (double) list.get("temp_c");

我有错误

java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer

1 个答案:

答案 0 :(得分:3)

试试这个。

先改为if #available(iOS 10.0, *) { UNUserNotificationCenter.current().delegate = self } else { // Fallback on earlier versions } // ios 10 if #available(iOS 10.0, *) { let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .sound,.badge]) { (granted, error) in // actions based on whether notifications were authorized or not } UIApplication.shared.registerForRemoteNotifications() } // iOS 9 support else if #available(iOS 9, *) { UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)) UIApplication.shared.registerForRemoteNotifications() } else { let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) UIApplication.shared.registerUserNotificationSettings(settings) UIApplication.shared.registerForRemoteNotifications() } } ,然后使用String解析

Double.parseDouble