Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 带有嵌套JSON的valueForKeyPath_Ios_Json_Swift_Key Value_Alamofire - Fatal编程技术网

Ios 带有嵌套JSON的valueForKeyPath

Ios 带有嵌套JSON的valueForKeyPath,ios,json,swift,key-value,alamofire,Ios,Json,Swift,Key Value,Alamofire,我在使用valueForKeyPath时遇到了一个问题,在这里,我获取dataVersion值的方法非常有效: Alamofire.request(.GET, "https://www.amazon.fr/s", parameters:parameters) .responseJSON { (_, _, JSON, _) -> Void in let priceResult: String? = JSON?.valueForKeyPath("reponseMetad

我在使用valueForKeyPath时遇到了一个问题,在这里,我获取dataVersion值的方法非常有效:

 Alamofire.request(.GET, "https://www.amazon.fr/s", parameters:parameters)
   .responseJSON { (_, _, JSON, _) -> Void in

       let priceResult: String? = JSON?.valueForKeyPath("reponseMetadata.dataVersion") as String?
       println(priceResult)

 }
但当我尝试像这样访问url值时,它失败了

 Alamofire.request(.GET, "https://www.amazon.fr/s", parameters:parameters)
   .responseJSON { (_, _, JSON, _) -> Void in

       let priceResult: String? = JSON?.valueForKeyPath("preloadImages.images.url") as String?
       println(priceResult)

 }
这里是我的Json:

{
    responseMetadata: {
      dataVersion: "v0.1"
    },
    preloadImages: {
      images: [
          {
              url: "http://ecx.images-amazon.com/images/I/51K4P7REBKL._SL500_AC_.jpg"
          }
      ]
    }
}

我是IOs的新手,所以欢迎任何帮助

预加载图像。图像是一个对象数组(swift speak中的字典数组),因此您的
值forkeyPath
无法工作。不幸的是,没有任何方法可以通过valueForKeyPath对数组进行索引,因此您必须减少对数组的直接获取:

let string = "{ \"responseMetadata\": { \"dataVersion\": \"v0.1\" }, \"preloadImages\": { \"images\": [ { \"url\": \"http://ecx.images-amazon.com/images/I/51K4P7REBKL._SL500_AC_.jpg\" } ] } }"
var error : NSError?
let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
if let json: AnyObject = NSJSONSerialization.JSONObjectWithData(data!, options: .allZeros, error: &error) {
    if let images = json.valueForKeyPath("preloadImages.images") as? Array<Dictionary<String,String>> {
        let url = images[0]["url"]

        println("url = \(url)")
    }
} else {
    println("json failed: \(error)")
}
let string=“{\”responseMetadata\”:{\“dataVersion\”:\“v0.1\”,“preload images\”:{\“images\”:[{\“url\”:\”http://ecx.images-amazon.com/images/I/51K4P7REBKL._SL500_AC_.jpg\" } ] } }"
var错误:N错误?
让data=string.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion:true)
如果让json:AnyObject=NSJSONSerialization.JSONObjectWithData(data!,选项:.allZeros,错误:&error){
如果让images=json.valueForKeyPath(“preload-images.images”)作为?数组{
让url=图像[0][“url”]
println(“url=\(url)”)
}
}否则{
println(“json失败:\(错误)”)
}
请注意,您的JSON也不是如图所示的有效,因为没有引用对象键,我假设您正在使用println转储JSON变量,而不是显示实际的JSON数据