json - Swift:访问 NSDictionairy 时 JSON 为 nil

标签 json swift alamofire

当我打印(JSON)时,我得到了文件,所以.request有效。 但是当我尝试访问“test” key (存在)时,我得到 nil

我明白

"I am here"
"now ,I am here"

Alamofire.request(.GET, self.APIBaseUrl , parameters: ["api_key": self.APIkey])
            .responseJSON { response in
                if let JSON = response.result.value {
                  print("I am here")
                  if let str = JSON as? NSDictionary {
                     print("now , I am here")
                     if let movieUrlString = str["poster_path"] as? String)! {
                        print("but now here")
                        }

已编辑

print(dict)


**dates** =     {
maximum = "2015-10-21";
minimum = "2015-09-30";
};
page = 1;
**results** =     (
            {
 adult = 0;
"poster_path" = "/2XegKZ0I4QrvzpEHneigRk6YTB1.jpg";

++(更多结果)

最佳答案

尝试使用更有意义的调试打印语句和变量名称。此外,您没有使用正确的变量进行下标。固定示例:

Alamofire.request(.GET, self.APIBaseUrl , parameters: ["api_key": self.APIkey]).responseJSON { response in
    if let JSON = response.result.value {
        print("inside JSON result")
        if let dict = JSON as? NSDictionary {
            print("inside decoded JSON")
            if let results = dict["results"] as? [NSDictionary] {
                for result in results {
                    if let movieUrlString = result["poster_path"] as? String {
                        print(movieUrlString)
                    }
                }
            }
        }
    }
}

关于json - Swift:访问 NSDictionairy 时 JSON 为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32822436/

相关文章:

android - 在 android spinner 中填充 json 数组

javascript - 如何强制 jquery attr() 添加带有单引号的属性

javascript - 字符串到 Ivalue 的转换

ios - 将标签栏快速移动到屏幕顶部

java - Android JSON解析问题: seems to separate it into 2 separate objects and only recognizes one

ios - 无法获取类 uitableview Swift 3 上的数据

ios - 用户界面标签!出现而不是隐藏在 Swift 中

ios - 如何在 Alamofire 中设置传输编码

ios - 如何为 Alamofire 请求全局设置基本 url?

ios - Alamofire 返回与预期不同的结果