gpt4 book ai didi

ios - Error Domain=NSCocoaErrorDomain Code=3840 在Swift5中解析JsonString时

转载 作者:行者123 更新时间:2023-11-28 13:27:24 29 4
gpt4 key购买 nike

我收到一条推送消息。好像是jsonString。

但这并没有改成jsonArray。有什么问题?

AppDelegate.swift

        guard
let aps = data[AnyHashable("aps")] as? NSDictionary,
let alert = aps["alert"] as? NSDictionary,
let body = alert["body"] as? String
else {
Log.Error("it's not good data")
return
}
let jsonStringData : Data = body.data(using: .utf8)!
Log.Info(jsonStringData)
do {
let jsonArray = try JSONSerialization.jsonObject(with: jsonStringData, options : .allowFragments) as? [Dictionary<String,Any>]

Log.Info(jsonArray) // use the json here

} catch let error as NSError {
Log.Error(error)
}

正文值(字符串)

{ 
notification : {

"title" : "test title.",

"body" : "test context."

},

data : {

"image" : "http://11.111.111.111:100000000/_img/sample_01.jpg",

"page_url" : "http://11.111.111.111:100000000/test?address=",

"type" : "test"

}
}

body的日志是这样的,JSON形式。但这不是翻译的。我不知道我错过了什么。

**错误是:**

Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 7." UserInfo={NSDebugDescription=No string key for value in object around character 7.}

最佳答案

感谢@vadian、@Larme、@Sh_Khan 和@Kamran

如您所说,JSON 无效。这应该由 JSON 接收。正如@Larme 所说,我不需要选项,我不需要设置as。它反而向我显示了一个警告。

成功了

        guard
let aps = data[AnyHashable("aps")] as? NSDictionary,
let alert = aps["alert"] as? NSDictionary,
let body = alert["body"] as? String
else {
Log.Error("it's not good data")
return
}
let jsonStringData : Data = body.data(using: .utf8)!
Log.Info(jsonStringData)
do {
let json = try JSONSerialization.jsonObject(with: jsonStringData, options: [])

Log.Info(json) // use the json here
guard let jsonArray = json as? [String: Any] else {
return
}
Log.Debug(jsonArray)
Log.Debug(jsonArray["data"])

} catch let error as NSError {
Log.Error(error)
}

关于ios - Error Domain=NSCocoaErrorDomain Code=3840 在Swift5中解析JsonString时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58115266/

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