json - 类型 any 没有下标成员 swift 3

标签 json swift swift3 alamofire

我的代码是这样的

import UIKit
import Alamofire

class ViewController: UIViewController {


var young = "https://jsonplaceholder.typicode.com/posts"


override func viewDidLoad() {
    super.viewDidLoad()

    callAlamo(url: young)
}

func callAlamo(url: String){

    Alamofire.request(url).responseJSON { (response) in
        let responseJSON = response.data
        if responseJSON  == nil{
           print("response is empty")
        }else{
            print("Jon is \(responseJSON)")

            self.parseJson(JSONData: responseJSON!)
        }

    }
}

func parseJson(JSONData: Data){

    do{
        let readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers)

        for  i in 0..<(readableJSON as AnyObject).count{
            print(readableJSON[i] as String)
        }

    }catch{
        print(error)
    }
}  
}

enter image description here

我需要这个 Json 中的每个数组元素。

最佳答案

尝试使用下面的代码:

Alamofire.request(url).responseJSON { (response) in

        switch response.result {
        case .success(let value) :

            print(response.request)  // original URL request
            print(response.response) // HTTP URL response
            print(response.data)     // server data
            print(response.result)   // result of response serialization

            if let JSON = response.result.value as! [String:AnyObject]!{
                print("JSON: ",JSON)
            }
        case .failure(let encodingError):
            completionHandler(APIResponse.init(status: "failure", response: nil, result:nil))
        }
    }

关于json - 类型 any 没有下标成员 swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39792194/

相关文章:

android - 从 AsyncTask 更新两个独立的小部件

swift - 如何修复 Xcode ARKit 中类型 'SCNNode' 的值没有成员 'materials'

javascript - JS,JSON : How to get the n first items respecting 2 conditions?

javascript - 删除嵌套的 json 元素并显示为单独的键值对

swift - 在窗口关闭时停止异步操作

ios - 需要修复我的复选框 : to change states in one click instead of two clicks. Swift 3, IOS

swift 3 : how to handle precedencegroup now operator should be declare with a body?

ios - 在闭包中使用可变参数时编译器崩溃

java - Jackson JSON 序列化映射

swift - 使用索引将 Swift 数组转换为字典