我正在尝试将这个字符串转换成字典
{
"sender_id": 7,
"Sender_name": Testchumthree Tester,
"message": 42,
"Sender_image": https://graph.facebook.com/v2.10/281359099024687/picture?type=normal,
"timestamp": "0",
"group_id": 50
}
以下是我到目前为止发现的内容。
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
但是我得到一个错误,说数据格式不正确。任何帮助将非常感激。
最佳答案
您的 JSON 的问题是字符串格式不正确。它们周围必须有引号,并且根据库的不同,“/”也必须被转义。
尝试使用这个:
{
"sender_id": 7,
"Sender_name": "Testchumthree Tester",
"message": 42,
"Sender_image": "https:\/\/graph.facebook.com\/v2.10\/281359099024687\/picture?type=normal",
"timestamp": "0",
"group_id": 50
}
关于json - 如何使用 swift 3 将 json 字符串转换为字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47695020/