gpt4 book ai didi

json - 如何使用 swift 3 将 json 字符串转换为字典?

转载 作者:行者123 更新时间:2023-11-28 15:10:41 25 4
gpt4 key购买 nike

我正在尝试将这个字符串转换成字典

{
"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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com