gpt4 book ai didi

ios - JSONSerialization.jsonObject 返回 nil

转载 作者:行者123 更新时间:2023-11-28 14:09:04 25 4
gpt4 key购买 nike

var responseString = String(data: data, encoding: .utf8)
var responseDict: [AnyHashable : Any]? = nil
if let anEncoding = responseString?.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
responseDict = try! JSONSerialization.jsonObject(with: anEncoding, options: .mutableContainers) as? [AnyHashable : Any]
}

responseString 结果是:

[{\"message_id\":1916,\"in_app\":{\"primary_color\":\"#333333\",\"secondary_color\":\"#ffffff\",\"position\":\"0\",\"duration\":\"30\",\"expiry\":\"2018-11-07T23:17:46.169-05:00\",\"delay_minute\":\"0\",\"delay_second\":\"0\",\"message\":\"This is what your message will look like! Type in your message in the text area and get a preview right here\",\"data\":{},\"deeplinkurl\":\"\",\"attachment-url\":\"\",\"title\":\"\",\"subtitle\":\"\"}},{\"message_id\":1920,\"in_app\":{\"primary_color\":\"#333333\",\"secondary_color\":\"#ffffff\",\"position\":\"0\",\"duration\":\"30\",\"expiry\":\"2018-11-08T03:52:15.404-05:00\",\"delay_minute\":\"0\",\"delay_second\":\"0\",\"message\":\"This is what your message will look like! Type in your message in the text area and get a preview right here\",\"data\":{},\"deeplinkurl\":\"\",\"attachment-url\":\"\",\"title\":\"\",\"subtitle\":\"\"}}]

responseDict 结果为 nil
相应的 Objective-C 代码工作正常。

最佳答案

这很好用……

let json = """
[{\"message_id\":1916,\"in_app\":{\"primary_color\":\"#333333\",\"secondary_color\":\"#ffffff\",\"position\":\"0\",\"duration\":\"30\",\"expiry\":\"2018-11-07T23:17:46.169-05:00\",\"delay_minute\":\"0\",\"delay_second\":\"0\",\"message\":\"This is what your message will look like! Type in your message in the text area and get a preview right here\",\"data\":{},\"deeplinkurl\":\"\",\"attachment-url\":\"\",\"title\":\"\",\"subtitle\":\"\"}},{\"message_id\":1920,\"in_app\":{\"primary_color\":\"#333333\",\"secondary_color\":\"#ffffff\",\"position\":\"0\",\"duration\":\"30\",\"expiry\":\"2018-11-08T03:52:15.404-05:00\",\"delay_minute\":\"0\",\"delay_second\":\"0\",\"message\":\"This is what your message will look like! Type in your message in the text area and get a preview right here\",\"data\":{},\"deeplinkurl\":\"\",\"attachment-url\":\"\",\"title\":\"\",\"subtitle\":\"\"}}]
""".data(using: .utf8)!

do {
let object = try JSONSerialization.jsonObject(with: json, options: [])
print(object)
} catch {
print(error)
}

…但不要使用 JSONSerialization…声明代表您的数据的自定义对象并使用 Decodable 代替

关于ios - JSONSerialization.jsonObject 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52717453/

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