gpt4 book ai didi

如果我尝试将其转换为其他任何内容,JSON 序列化将返回 nil

转载 作者:行者123 更新时间:2023-11-30 12:08:04 25 4
gpt4 key购买 nike

我在解析这个 json 对象时遇到问题。我能够将它解析为 Any 。但如果我尝试将其转换为 NSDictionary 或其他任何内容,它会返回 nil。谁能帮我?我咨询了几个问题,它们都给出了相同的零结果。

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let reponse = response {
print(reponse)
}
if let data = data {
//print(data)

do{

// let json = try JSONSerialization.jsonObject(with: data, options: [])


let json = try JSONSerialization.jsonObject(with: data, options:.allowFragments)

// do stuff here

// accountJson = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary

print("\(json)") // This one prints .

let accountJson = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary

print("trying to print accountjson")
print("\(accountJson)") // this one doesnt.




} catch {
print(error)
}

}

}
task.resume()

第一个 print 语句起作用并在终端中生成以下代码。

(
{
"__v" = 0;
"_id" = 59cba54406e2661530c1aa6e;
pass = ra;
type = ra;
username = NikRa;
}
)

最佳答案

大家好,我确实弄清楚了。我必须将其转换为数组中的数组。

let accountJson = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as?  [[String: Any]]

关于如果我尝试将其转换为其他任何内容,JSON 序列化将返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46453858/

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