Swift:访问NSDictionairy时JSON为nil

时间:2015-09-28 11:58:54

标签: json swift alamofire

当我打印(JSON)时,我得到了文件,因此.request有效。 但是当我试图访问"测试" key(存在)我得零

我得到了

  

"我在这里"
  "现在,我在这里"

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")
                        }

EDITED

print(dict)


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

++(更多结果)

1 个答案:

答案 0 :(得分:1)

尝试使用更有意义的调试打印语句和变量名称。您也没有使用正确的变量进行下标。修复示例:

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)
                    }
                }
            }
        }
    }
}