所以,我有从通知中获得的 userInfo 数据,我想解码这些数据并将其传递给另一个类。我使用 swiftyJSON 作为 json 解码器。这是我的代码:
func decodedNotificationTransactionData(data: Any, completionHandler: @escaping (_ dict: [String : Any]) -> ()){
let dict = JSON(data)
print ("this is json data \(dict)")
print("this is status \(dict["status"])")
let test = [
"id" : "\(dict["id"])",
"orderDate" : "\(dict["orderDate"])",
"patientName" : "\(dict["patient"]["fullName"])",
"patientAddress" : "\(dict["patient"]["address"])",
"caregiverName" : "\(dict["care"]["fullName"])",
"caregiverAddress" : "\(dict["care"]["address"])",
"serviceType" : "\(dict["service"]["label"])",
"status" : "\(dict["status"])",
"price" : "\(dict["totalAmount"])",
"otherNote" : "\(dict["otherNote"])",
"totalService" : dict["detailDatas"].count,
"serviceLongType" : dict["detailDatas"][0]["type"],
"jsonContent" : dict
] as [String : Any]
completionHandler(test)
}
this the "print ("this is json data (dict)")" result
当代码到达print("this is json data (dict)")时,数据在那里没有问题,但是当代码到达print("this is status (dict[ "status"])") 它在日志中给出 null。您遇到过同样的问题吗?
最佳答案
快速查看 SwiftyJSON 文档会立即发现错误:
Creates a JSON object.
- parameter object: the object.
- note: this does not parse aString
into JSON, instead useinit(parseJSON: String)
.
- returns: the created JSON objectpublic init(_ object: Any) {
所以你必须使用
let dict = JSON(parseJSON: data)
关于ios - 奇怪的 json 解码器行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48502325/