1

I am getting the following JSON from Foursquare API and I have been struggling with extracting the data:

{
  "meta":{
    "code":200,
    "requestId":"58122e59498e5506a1b23580"
  },
  "response":{
    "venues":[
      {
        "id":"4d56c381a747b60cd4a12c2b",
        "name":"Sports Circle",
        "contact":{},
        "location":{
          "lat":31.9,
          "lng":35.9,
          "labeledLatLngs":[
            {
              "label":"display",
              "lat":31.9,
              "lng":35.90
            }
          ],
      ],
      "confident":true
    }
  }
}

I want to get the name in venues in addition to the lat and lng values. I have tried this so far but it gets out of the second if statement at JVenues because it is nil:

func parseData (JSONData: Data){
    do {
        var readableJson = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! [String:AnyObject]

        if let JResponse = readableJson ["response"] as? [String:AnyObject] {
            if let JVenues = JResponse["venues"] as? [String:AnyObject]{
                if let JName = JVenues["name"] as? String{
                    NSLog(JName)
                }
            }
        }
    } catch {
        print(error)
    }
}

1 Answer 1

2

This is what the other answers are getting at. Will probably make more sense if you can see it all laid out...

if let JResponse = readableJson ["response"] as? [String : AnyObject] {
    if let JVenues = JResponse["venues"] as? [[String : AnyObject]] {
        if let JName = JVenues.first?["name"] as? String {
            NSLog(JName)
        }
    }
}

Note this only gets the FIRST name in the array of venues.

EDIT:

I prefer something like this. Define a struct and convert your dictionaries to the struct:

struct Venue {
    var name: String?
    var venueId: String?

    init(_ venueDictionary: [String : AnyObject]) {
        self.name = venueDictionary["name"] as? String
        self.venueId = venueDictionary["id"] as? String
    }
}

In your class create a property such as:

var venues = [Venue]()

From your JSON map the dictionaries to the venue array. I renamed variables that start with a capital for convention.

if let response = readableJson ["response"] as? [String : AnyObject] {
     if let responseVenues = response["venues"] as? [[String : AnyObject]] {
         self.venues = responseVenues.map({ Venue($0)) })
     }
 }

Use anywhere in your class like:

let venue = self.venues.first
print(venue?.name)

Or:

if let venue = self.venues.find({ $0.name == "Sports Circle" }) {
    print("found venue with id \(venue.venueId)")
}
Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your clarification that worked! I am sorry I am new to swift if I want to access the values in name in a variable it is not clear to me how can I do that? is there a link or a tutorial on how to deal with this?
Added additional help. If my answer was helpful to you you can up vote it. Thanks!
If you need something more permanent, like info you can access throughout the app and on multiple runs, you'll want to learn Core Data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.