swiftyJson 解析json

问答cw0925 • 于 2017-03-09 22:08:48 +0800 • 最后由 phoenix1332017-03-23 18:14:48 +0800 1394 阅读
        let dict = JSON(response)
        if let data = dict["data"].dictionary {
            if let items = data["items"]?.arrayObject {
                for item in items {
                    if let itemData = item["data"] {
                        let product = DPModel(dict: itemData as! [String: AnyObject])
                        items.append(product)
                    }
                }
            }
        }

在if let itemData = item["data"]报 type type any has no subscript members 错误,在swift2.3上正常在swift3.0就报错,该怎么改刚刚学习swift。(json格式) { "code" : 200, "data" : { "items" : [ { "data" : { "brandid" : null, "brandorder" : 0, "categoryid" : null, "coverimageurl" : "http://7fvaoh.com3.z0.glb.qiniucdn.com/image/150819/a7wjwjck3w.jpg-w720", "createdat" : 1439952881, "description" : "开关一键式,便携又防漏,热水氤氲的湿气打在脸上,温暖又可爱~", "editorid" : 1037, "favoritescount" : 202, "id" : 1355, "imageurls" : [ "http://7fvaoh.com3.z0.glb.qiniucdn.com/image/150819/ovabcvdgu.jpg-w720", "http://7fvaoh.com3.z0.glb.qiniucdn.com/image/150819/gaw35u5bt.jpg-w720", "http://7fvaoh.com3.z0.glb.qiniucdn.com/image/150819/q8t6cumc8.jpg-w720", "http://7fvaoh.com3.z0.glb.qiniucdn.com/image/150819/725ba3h13.jpg-w720", "http://7fvaoh.com3.z0.glb.qiniucdn.com/image/150819/n482l8tkb.jpg-w720", "http://7fvaoh.com3.z0.glb.qiniucdn.com/image/150819/a7wjwjck3w.jpg-w720" ], "isfavorite" : false, "name" : "不锈钢保温杯", "postids" : [], "price" : "58.00", "purchaseid" : "43879526968", "purchasestatus" : 1, "purchasetype" : 2, "purchaseurl" : "http://s.click.taobao.com/t?e=m%3D2%26s%3DYxYY5WFio4EcQipKwQzePOeEDrYVVa64pRe%2F8jaAHci5VBFTL4hn2ZzpsqmXu%2FE9WI6w0dMGH8htabAtTg06px%2FVXctKptzdeP1OMBEVuKXSfUtBr7Z5QILBwQLXKLHMyxRpeaHkj4I49d%2BuVtoyMHEqY%2Bakgpmw", "subcategoryid" : null, "updated_at" : 1439952881, "url" : "http://dantang.liwushuo.com/items/1355" }, "type" : "item" },

"message" : "OK" }

回复: 1
  • phoenix133 2017-03-23 18:14:48 +0800

    我觉得你应该在行号是2的那一行(也就是第三行)加上as! [String:Any]

  • 请注意单词拼写,以及中英文排版,参考此页
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
  • 支持表情,见 Emoji cheat sheet
  • @name 会链接到用户页面,并会通知他
  • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif
Ctrl+Enter