【发布时间】:2017-05-18 10:36:19
【问题描述】:
我正在为 iPhone 开发一个应用程序。我已成功将数据输入应用程序。我遇到的问题是 JSON 中的某些数据结构与其他数据结构有些不同,因此它不会进入或不让我的数据显示。到目前为止我的代码是:
更新
导致问题的 JSON 结构
categories: [
{
name: "Here and now"
}
],
视图控制器中的 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)
所以毕竟它不起作用,视图控制器中的输出是空的,我确信这与我的代码没有正确读取 JSON 中的数据结构有关。
有什么想法吗?
【问题讨论】:
-
请阅读 JSON(不是反序列化后的字典表示!)。这很简单:
{}是一个字典,[]是一个数组。所有密钥都必须是String。双引号中的所有值都是String,带小数位的数值是Double,没有的是Int,true/false(不是双引号)是Bool,null是NSNull.这就是 JSON 的整个类型集。 -
PS:如果您想获得 real 解决方案,请发布 real JSON(字符串 - 至少是 real键和 real 值类型)而不是不相关的伪代码,并发布数据源的填充方式。例如 real JSON 似乎包含 HTML 标签。
标签: arrays iphone json swift swift3