Swift - 如何从 json 获取数据类型
Swift -How to get datatype from json
我在后端使用 firebase,当我保存时间戳时,我将其另存为
let timeIntervalSince1970 = Date().timeIntervalSince1970 // 1563651621.760651
var dict = [String: Any]()
dict.updateValue(timeIntervalSince1970, forKey: "timeStamp")
ref.updateChildValues(dict)
稍后我解析它:
guard let dict = snapshot.value as? [String: Any] { else return }
let timeStamp = dict["timeStamp"] as? Double ?? 0 // 1563651621.760651
在为静默推送通知保存日期时,我是这样保存的
let timeIntervalSince1970 = Date().timeIntervalSince1970 // 1563651621.760651
let date = Int64(timeIntervalSince1970 * 1000) // 1563651621760
dataDict.updateValue(date, forKey: "timeStamp")
// other values ...
paramDict.updateValue(dataDict, forKey: "data")
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: paramDict, options: [.prettyPrinted])
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("key=\(serverKey)", forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
do {
if let jsonData = data {
if let jsonDataDict = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: AnyObject] {
Print("Received data:\n\(jsonDataDict))")
}
}
} catch let err as NSError {
print(err.debugDescription)
}
}
task.resume()
但是当我尝试从 userInfo 获取时间戳的数据类型时,我可以解析除日期之外的所有其他值:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let info = notification.request.content.userInfo
guard let userInfo = info as? [String: Any] else { return }
for (k,v) in userInfo {
print(k) // when k == timeStamp
print(v) // the value printed is 1563651621760
}
let dbl = userInfo["timestamp"] as? Double ?? 0
let nsn = userInfo["timestamp"] as? NSNumber ?? 0
let fl = userInfo["timestamp"] as? Float ?? 0
let in = userInfo["timestamp"] as? Int ?? 0
let uin = userInfo["timestamp"] as? UInt ?? 0
let in64 = userInfo["timestamp"] as? Int64 ?? 0
print(dbl) // 0.0
print(nsn) // 0
print(fl) // 0.0
print(in) // 0
print(uin) // 0
print(in64) // 0
如何获取数据类型以便解析日期(获取数据类型后我可以自己将其转换回 Date 对象)?
逻辑上如果不是数字类型就一定是字符串
let str = userInfo["timestamp"] as? String ?? ""
我在后端使用 firebase,当我保存时间戳时,我将其另存为
let timeIntervalSince1970 = Date().timeIntervalSince1970 // 1563651621.760651
var dict = [String: Any]()
dict.updateValue(timeIntervalSince1970, forKey: "timeStamp")
ref.updateChildValues(dict)
稍后我解析它:
guard let dict = snapshot.value as? [String: Any] { else return }
let timeStamp = dict["timeStamp"] as? Double ?? 0 // 1563651621.760651
在为静默推送通知保存日期时,我是这样保存的
let timeIntervalSince1970 = Date().timeIntervalSince1970 // 1563651621.760651
let date = Int64(timeIntervalSince1970 * 1000) // 1563651621760
dataDict.updateValue(date, forKey: "timeStamp")
// other values ...
paramDict.updateValue(dataDict, forKey: "data")
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: paramDict, options: [.prettyPrinted])
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("key=\(serverKey)", forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
do {
if let jsonData = data {
if let jsonDataDict = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: AnyObject] {
Print("Received data:\n\(jsonDataDict))")
}
}
} catch let err as NSError {
print(err.debugDescription)
}
}
task.resume()
但是当我尝试从 userInfo 获取时间戳的数据类型时,我可以解析除日期之外的所有其他值:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let info = notification.request.content.userInfo
guard let userInfo = info as? [String: Any] else { return }
for (k,v) in userInfo {
print(k) // when k == timeStamp
print(v) // the value printed is 1563651621760
}
let dbl = userInfo["timestamp"] as? Double ?? 0
let nsn = userInfo["timestamp"] as? NSNumber ?? 0
let fl = userInfo["timestamp"] as? Float ?? 0
let in = userInfo["timestamp"] as? Int ?? 0
let uin = userInfo["timestamp"] as? UInt ?? 0
let in64 = userInfo["timestamp"] as? Int64 ?? 0
print(dbl) // 0.0
print(nsn) // 0
print(fl) // 0.0
print(in) // 0
print(uin) // 0
print(in64) // 0
如何获取数据类型以便解析日期(获取数据类型后我可以自己将其转换回 Date 对象)?
逻辑上如果不是数字类型就一定是字符串
let str = userInfo["timestamp"] as? String ?? ""