gpt4 book ai didi

arrays - Swift 3 中的数据字段无法正确显示 JSON 数据

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

我正在为 iPhone 开发一个应用程序。我已成功将数据输入应用程序。我遇到的问题是 JSON 中的某些数据结构与其他数据结构有点不同,因此它不会进入或不允许我的数据显示。到目前为止我的代码是:

已更新

导致问题的 JSON 结构

categories: [
{
name: "Here and now"
}
],

View Controller 中的 Swift 代码

guard let dic2 = post["categories"] as? [[String: Any]] else {
return
}
for category in dic2 {
guard let content = category["name"] as? String else {
return
}
}

然后下面的代码是追加postinput

postsinput.append(postinput.init(mainImage: mainImage, name: title, author: author, summary: summary, content: content, categories: content))

然后下面就是将输出放到一个标签中

let catLabel = cell?.viewWithTag(4) as! UILabel
catLabel.text = (postsinput[indexPath.row].categories).replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)

所以毕竟它不起作用, View Controller 中的输出是空的,我确信这与我的代码没有正确从 JSON 读取数据结构有关。

有什么想法吗?

最佳答案

假设你的 JSON 是这样的

{
"categories": [{
"name": "Here and now"
}]
}

categories 不是字典,它实际上是字典对象的数组。所以这一行将会失败:

guard let dic2 = post["categories"] as? [String: Any]

应该是

guard let dic2 = post["categories"] as? [[String: Any]]

关于arrays - Swift 3 中的数据字段无法正确显示 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44045418/

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