1

I'm very new to Swift and have spent several hours just trying to pull the photo_url key out of a JSON response.

I'm using this for the reading the JSON:

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

Then:

                if let eventsDictionary = jsonDictionary {
                    let upcomingEvents = UpcomingEvents(eventsDictionary: eventsDictionary)
                    completion(upcomingEvents)
                } else {
                    completion(nil)
                }

Here is my (failed) attempt to pull out the key:

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

enter image description here

I changed "[String: Any]" to [String: AnyObject] and now i get this... enter image description here

3
  • What does your JSON response look like? What error do you get? Commented Nov 24, 2017 at 1:37
  • Why use NSArray? There's no good reason for that. Cast the array to a Swift array type instead (probably [String] since that's what the array seems to contain). Also, use as? rather than as!, and handle the case where you get nil. Commented Nov 24, 2017 at 1:40
  • I added a screenshot of the response Commented Nov 24, 2017 at 1:45

4 Answers 4

2

There are problems casting Any to NSArray. Just make your Init method taking [String:AnyObject]. But, better use Array instead of NSArray here

Sign up to request clarification or add additional context in comments.

2 Comments

I'made the changes you suggested but im still not able to pull out the last photo_url field
@Verty00 it’s because of .mutableContainer option. Read more about serializing options. Here you don’t need them
0

Try to get url use following code.

let firstObj  = groups[0] as! [String: String] // use if let to unwrap is better
let url = firstObj["photo_url"]

1 Comment

Forgot to say the first posted answer's suggestion is very good for your programming.
0

To get "photo_url" from the json file in your photo,

it looks like this:

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

Comments

0

You need to read json as `[String: Any].

 if let eventsDictionary = json as? [String: Any] {
     let upcomingEvents = UpcomingEvents(eventsDictionary: eventsDictionary)
     completion(upcomingEvents)               
 }

Then, init your UpcomingEvents model like this

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.