Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jason Gong
4,537 Pointsaccessing JSON data in swift
I'm currently using the google geocode api to get some info on an address the user types. The response is in the form of a JSON result:
How can I access the items in the value part of the dictionary? I would like to store that as a seperate array.
I've tried the following to no avail
let searchURL = NSURL(string:"https://maps.googleapis.com/maps/api/geocode/json?address=\(nospaceQuery!)&key=\(mapAPIKey)")
//Request made
var request1: NSURLRequest = NSURLRequest(URL: searchURL!)
let queue:NSOperationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
var err: NSError
//converting to JSON Object
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
jsonResult.objectForKey("value")
jsonResult.objectForKey("formatted_address")
I've also tried valueForKey.
Jason Gong
4,537 Pointsposted! : D
2 Answers
Stone Preston
42,016 Pointsalright im not 100% certain but it looks like the initial JSON dictionary (assigned to your jsonResult variable) has 2 key value pairs. one is called resullts. so you might need to do something like:
var results = jsonResult.objectForKey("results")
var value = results.objectForKey("value")
Jason Gong
4,537 Pointsit seems to always return nil when i try to access the JSON dictionary with objectForKey. hmm
Stone Preston
42,016 Pointsmaybe try
var results = jsonResult["results"]
var value = results["value"]
Jason Gong
4,537 Pointsthat gives me
x AnyObject? (instance_type = Builtin.RawPointer = 0xffff8062661b4d50)
as the value for results. Maybe the object shouldnt be an NS Dictionary? I am casting it as one for the return type.
Stone Preston
42,016 Pointstry casting results then as well. it definitely needs to be a dict:
var results = jsonResult["results"] as NSDictionary
var value = results["value"]
Jason Gong
4,537 PointsI'm using the geocode to get the location of a search I'm letting my users do on my app. They type a string into a searchbar and it returns the coordinates and the name of the location.
Do you think theres a better way to do what I'm trying to do?
Jason Gong
4,537 PointsIt's working now for valueForKey when i println it out to the console (it shows the nested series of { [] }. But if I try to iterate through it, it will error with a bad access error. In the debugger console it shows only a pointer and not the nested series of NSDictionaries and NSArrays like my JSON result.
Jason Gong
4,537 PointsWoo i figured it out. just had to use valueForKey and dig through all the nested objects. <3 Preston
Stone Preston
42,016 Pointsawesome glad you got it working
Jason Gong
4,537 PointsAlso not working it is still showing the pointer as whats in results. I did see some posts on stackoverflow that show ppl going through a lot of nested for loops to get at the data, but I can't seem to do that either.

Stone Preston
42,016 PointsStone Preston
42,016 Pointscan you post your code where you set your jsonResult variable?