swift 中 [String: AnyObject] 中带双引号的浮点值 returns
Float value returns with double quotes from [String: AnyObject] in swift
我不知道为什么我使用[String: AnyObject]
时浮点值总是带有双引号。你们有什么方法可以让我得到正确的结果吗?
let appliedLoyalty: Float = 1.05
let appliedWallet: Float = 0.55
let payLoad: [String: AnyObject] = ["custid": custid! as AnyObject, "discounts": ["loyalty": appliedLoyalty,"wallet": appliedWallet] as AnyObject] // custid is string value
print(payLoad)
当我打印 payLoad 时,浮点值用双引号引起来。
["discounts": {
"loyalty" = "1.05";
"wallet" = "0.45";
}, "custid": "puma"]
当您使用 print(payLoad)
时,将调用词典的 description
方法。 description
方法的实现与控制台上打印的相同。您应该使用 JSONSerialization
来查看实际的 json,而不是打印字典。使用下面的代码或使用断点来检查 payLoad
中存储的内容
let jsonData = try? JSONSerialization.data(withJSONObject: payLoad, options: .prettyPrinted)
let jsonStr = String(data: jsonData!, encoding: .utf8)
print(jsonStr!)
将 AnyObject 替换为 Any,如下所示:
let appliedLoyalty: Float = 1.05
let appliedWallet: Float = 0.55
let custID = "puma"
let payLoad: [String: Any] = ["custid": custID, "discounts": ["loyalty": appliedLoyalty,"wallet": appliedWallet]] // custid is string value
print(payLoad)
我不知道为什么我使用[String: AnyObject]
时浮点值总是带有双引号。你们有什么方法可以让我得到正确的结果吗?
let appliedLoyalty: Float = 1.05
let appliedWallet: Float = 0.55
let payLoad: [String: AnyObject] = ["custid": custid! as AnyObject, "discounts": ["loyalty": appliedLoyalty,"wallet": appliedWallet] as AnyObject] // custid is string value
print(payLoad)
当我打印 payLoad 时,浮点值用双引号引起来。
["discounts": {
"loyalty" = "1.05";
"wallet" = "0.45";
}, "custid": "puma"]
当您使用 print(payLoad)
时,将调用词典的 description
方法。 description
方法的实现与控制台上打印的相同。您应该使用 JSONSerialization
来查看实际的 json,而不是打印字典。使用下面的代码或使用断点来检查 payLoad
let jsonData = try? JSONSerialization.data(withJSONObject: payLoad, options: .prettyPrinted)
let jsonStr = String(data: jsonData!, encoding: .utf8)
print(jsonStr!)
将 AnyObject 替换为 Any,如下所示:
let appliedLoyalty: Float = 1.05
let appliedWallet: Float = 0.55
let custID = "puma"
let payLoad: [String: Any] = ["custid": custID, "discounts": ["loyalty": appliedLoyalty,"wallet": appliedWallet]] // custid is string value
print(payLoad)