在 swift 中的 json 响应中获取计数(项目数)

Getting the count (number of items) in a json response in swift

我正在学习 swift。我在 swift 代码中使用了 ANetworking(Obj C 中的库)。我已经成功地 return 解析了 JSON。但是,我想找到 returned 的 json 项的计数。这是我到目前为止所做的:

//in viewDidAppear function
manager.GET(url,
        parameters: nil,
        success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
            self.jsonFunc(responseObject.description)

我在 Whosebug 中做了上面的类似这个问题:AFNetworking and Swift - Save json response

现在,在函数 jsonFunc 中,我试图这样获取 json 数据的计数:

func jsonFunc(data: AnyObject) {
    let count: Int? = data.count // I understand that data is AnyObject. How to typecast and get the number of data items here?
    if let ct = count {
        println(ct)
        for index in 0...ct-1 {

          if let parsedData = data[index] as? [String: AnyObject] {               
                    println(parsedData)
           }
        }
     }
 }

我这里doing/missing的转换错误是什么?

默认情况下,AFHTTPRequestOperationManager 将 responseSerializer 设置为 AFJSONResponseSerializer 实例,因此 responseObject 已经是您解析的 JSON 设置为字典。这是代码(经过重构以删除强制转换和键入中的一些冗余):

  func jsonLoaded(responseObject: AnyObject) {
        if let data = responseObject as? NSDictionary {
           let count = data.count
            print(count)
            for index in 0..<count {
                //data -- urls for the apps that the user has (for the current logged in user)
                if let parsedData = data[index] as? [String: AnyObject] {
                    print(parsedData)
                }
            }
        }
    }