【问题标题】:Swift 3 JSON get String from JSONArray does not workSwift 3 JSON 从 JSONArray 获取字符串不起作用
【发布时间】:2016-12-18 12:55:14
【问题描述】:

我尝试从我的 JSON 服务器响应中提取文件名的值。

if let attachments = itemDict["attachments"] as? [[String:String]] {
            for attachment in attachments {
               print(attachment["filename"])
            }
        }

这是我的 JSON 字符串。

"attachments":[{"filename":"000000_none-5.png","uploadDate":1482062835000,"imageRatio":0.0}
,{"filename":"000000_none-2.png","uploadDate":1482062839000,"imageRatio":0.0}]

它不起作用。我在做什么错,我真的不知道。它不会进入 for 循环。

提前感谢您提供任何有用的回复!

【问题讨论】:

    标签: ios json string object swift3


    【解决方案1】:

    字典不是[String:String],而是[String:Any],因为它们还包含IntDouble 值:

    if let attachments = itemDict["attachments"] as? [[String:Any]] {
        for attachment in attachments {
           print(attachment["filename"] as! String)
        }
    }
    

    【讨论】:

      【解决方案2】:

      也许尝试转换为[Dictionary<String, AnyObject>] 而不是[[String:String]]

      【讨论】:

        猜你喜欢
        • 2016-06-30
        • 1970-01-01
        • 1970-01-01
        • 2015-06-30
        • 2017-03-09
        • 1970-01-01
        • 1970-01-01
        • 2017-08-11
        • 2020-09-08
        相关资源
        最近更新 更多