将字符串转换为字典?
Convert string to dictionary?
我有一个 QR 码扫描仪,可以将 QR 码读取为字符串。没有将其检测为字典的选项。所以唯一的解决办法是将它转换成字典(我认为)。请记住,我正在使用 swift 并使用来自苹果的 AVFoundation。
此 QR 码将打印出 ["test": "test123"] 作为字符串。我如何将其转换为字典?
这是我想出的。虽然输出不是字典。
let test = "[\"test\": \"test123\"]"
let data = test.data(using: .utf8)!
do{
let output = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String:String]
print ("\(String(describing: output))")
}
catch {
print (error)
}
我有一个 QR 码扫描仪,可以将 QR 码读取为字符串。没有将其检测为字典的选项。所以唯一的解决办法是将它转换成字典(我认为)。请记住,我正在使用 swift 并使用来自苹果的 AVFoundation。
此 QR 码将打印出 ["test": "test123"] 作为字符串。我如何将其转换为字典?
这是我想出的。虽然输出不是字典。
let test = "[\"test\": \"test123\"]"
let data = test.data(using: .utf8)!
do{
let output = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String:String]
print ("\(String(describing: output))")
}
catch {
print (error)
}