Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
Json 在Swift中从数组中取出物品_Json_Swift - Fatal编程技术网

Json 在Swift中从数组中取出物品

Json 在Swift中从数组中取出物品,json,swift,Json,Swift,我是Swift的新手,花了几个小时试图从JSON响应中提取photo\uURL键 我用它来阅读JSON: let jsonDictionary = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) 然后: 以下是我(失败)拔出钥匙的尝试: init(eventsDictionary: [String : Any]) { //photoUrl = eventsDictionary[Event

我是Swift的新手,花了几个小时试图从JSON响应中提取
photo\uURL

我用它来阅读JSON:

let jsonDictionary = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
然后:

以下是我(失败)拔出钥匙的尝试:

init(eventsDictionary: [String : Any]) {
    //photoUrl = eventsDictionary[EventKeys.photoUrl] as? String
    let groups: NSArray = eventsDictionary["groups"] as! NSArray
    let url: String = groups[0]
   print("THIS IS YOUR RETURNED PHOTO URL--\(url)--END OF RETURNED PHOTO URL")
}

我将“[String:Any]”更改为[String:AnyObject],现在我得到了这个。。。

向NSArray强制转换任何内容时出现问题。只需使Init方法接受[String:AnyObject]。但是,这里最好使用Array而不是NSArray

尝试使用以下代码获取url

let firstObj  = groups[0] as! [String: String] // use if let to unwrap is better
let url = firstObj["photo_url"]
要从照片中的json文件中获取“photo_url”

看起来是这样的:

init(eventsDictionary: [String : Any]) {
   if let groups = eventsDictionary["groups"] as? [NSDictionary]{
        /*
        // Get All URL
        var urls : [String] = []
        for group in groups{
            if let url = group.value(forKey: "photo_url"){
                urls.append(url)
            }
        }
        */

        // Groups[0] url
        let url: String = groups[0].value(forKey: "photo_url") as! String
        print("THIS IS YOUR RETURNED PHOTO URL--\(url)--END OF RETURNED PHOTO URL")
    }
}

您需要将json读取为“[String:Any]

 if let eventsDictionary = json as? [String: Any] {
     let upcomingEvents = UpcomingEvents(eventsDictionary: eventsDictionary)
     completion(upcomingEvents)               
 }
然后,像这样初始化您的
UpcomingEvents
模型

init(eventsDictionary: [String : Any]) {
    let groups: NSArray = eventsDictionary["groups"] as! NSArray
    let group1 = groups[0] as! NSDictionary
    let photoURL = group1["photo_url"] as! String
    print(photoURL)
}

您的JSON响应是什么样子的?您会遇到什么错误?为什么要使用
NSArray
?这没有什么好的理由。改为将数组强制转换为Swift数组类型(可能是
[String]
,因为这似乎是数组包含的内容)。另外,使用
作为?
而不是
作为init(eventsDictionary: [String : Any]) {
    let groups: NSArray = eventsDictionary["groups"] as! NSArray
    let group1 = groups[0] as! NSDictionary
    let photoURL = group1["photo_url"] as! String
    print(photoURL)
}