gpt4 book ai didi

ios - Swift - 解析嵌套 JSON 对象中的数组不起作用

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

let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [.allowFragments])
if let responseJSON = responseJSON as? [String:Any] {
if let tJsonObj = xResponse["d"] as? [[String:Any]] {
// not working here...
}
}

tJsonObj 变量没有获取我的 json 数组内容。我的 json 看起来像这样:

{"d": "[{\"title\":\"xxx\",\"timestamp\":\"2017-10-16 23:53:40\"},{\"title\":\"Mein Test iPhone 7\",\"timestamp\":\"2017-10-17 18:16:24\"}]"}

我希望有人能提供帮助 - 谢谢!

最佳答案

d 的值是另一个 JSON 字符串。您需要使用 JSONSerialization 两次

do {
if let responseJSON = try JSONSerialization.jsonObject(with: data) as? [String:Any],
let tJsonObj = responseJSON["d"] as? String {
if let innerJSON = try JSONSerialization.jsonObject(with: Data(tJsonObj.utf8)) as? [[String:Any]] {
for item in innerJSON {
print(item)
}
}
}
} catch {
print(error)
}

关于ios - Swift - 解析嵌套 JSON 对象中的数组不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46801415/

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