ios - 无法将类型 '__NSArrayI' (0x10df73c08) 的值转换为 'NSDictionary' (0x10df74108)

标签 ios arrays json swift3 alamofire

    let url = "http://xyz/index_main.php?c=webservices&a=get&e=007"

    Alamofire.request(url, method: .post, parameters: nil, encoding: JSONEncoding.default)
        .responseJSON { response in
            print(response)
            //to get status code
            if let status = response.response?.statusCode {
                switch(status){
                case 201:
                    print("example success")
                default:
                    print("error with response status: \(status)")
                }
            }
            //to get JSON return value
            if let result = response.result.value {
                let JSON = result as! NSDictionary
                print(JSON)
            }

结果:

    {
    "create_by" = 007;
    "create_date" = "2016/06/20";
    "due_date" = "2016/06/22";
    "estimate_time" = "";
    fixer = 007;
    priority = High;
    "project_code" = testing;
    status = "Re-Open";
    "task_id" = 228;
    "task_title" = test;
    tester = 007;
},
    {
    "create_by" = 006;
    "create_date" = "2016/06/23";
    "due_date" = "2016/06/24";
    "estimate_time" = "";
    fixer = 007;
    priority = Critical;
    "project_code" = "tanteida.c";
    status = "In Progress";
    "task_id" = 234;
    "task_title" = testing;
    tester = 006;
}

我想转换为 NSDictionary 并获得不同的数组,如 task_id(array) 但我收到此错误:

"Could not cast value of type '__NSArrayI' to NSDictionary"

请帮助我

提前致谢

最佳答案

您的响应是 DictionaryArray 而不是 Dictionary,因此如果您想要所有字典,您可以使用循环访问它。

if let array = response.result.value as? [[String: Any]] {
    //If you want array of task id you can try like
    let taskArray = array.flatMap { $0["task_id"] as? String }
    print(taskArray)
}

关于ios - 无法将类型 '__NSArrayI' (0x10df73c08) 的值转换为 'NSDictionary' (0x10df74108),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40461888/

相关文章:

java - 无法理解 ObjectMapper writeValueAsString(Object value) 方法行为

objective-c - 从不同的 View Controller 访问属性

Python:从 IP 地址列表生成 IP 范围

android - 使用 gson 解析 JSON 对象

json - 如何从 Protractor 中的嵌套 JSON 结构读取数据

php - 计算重复项并使用分组项创建数组 | PHP

ios - 我如何与 Metal 沟通我正在避免 GPU 和 CPU 之间的数据冲突

ios - PFSignupViewController 可选电子邮件

ios - 当应用程序终止且电话被锁定时,VOIP 推送代表不会调用

c++ - 子数组赋值 : initialization with "{...}" expected for aggregate object error