Swift JSON Parsing - Unable to access field/returns nil -
i'm using google's geolocator api map stuff automatically. returns json string in request, i'm having lot of difficulty parsing it. i've tried things freddy , swiftyjson can't either extract field want.
here's sample of code:
func sendjsonrequest(connectionstring: string, httpmethod : httpmethod = httpmethod.get, jsonheaders : [string : string] = [ : ], jsonstring: string = "") -> nsdata? { // create request & response let request = nsmutableurlrequest(url: nsurl(string: connectionstring)!, cachepolicy: nsurlrequestcachepolicy.reloadignoringlocalcachedata, timeoutinterval: 5) // create json data , configure request let jsonstring = jsonstring; request.httpbody = jsonstring.datausingencoding(nsutf8stringencoding, allowlossyconversion: true) // handle both , post request.httpmethod = httpmethod.rawvalue // we'll sending json fine request.setvalue("application/json", forhttpheaderfield: "content-type") // add headers. if there aren't that's ok item in jsonheaders { request.addvalue(item.1, forhttpheaderfield: item.0) } print("request:") print(request) let session = nsurlsession.sharedsession() var data : nsdata? var urltask = session.datataskwithrequest(request) { (data, response, error) in data = data } urltask.resume() while (data == nil) { } return data } // return coordinates of given location func getcoordinates() -> coordinates { var result = coordinates() let connectionstring = "https://maps.googleapis.com/maps/api/geocode/json?address=43201" let jsondata = sendjsonrequest(connectionstring) let data = jsondata let json = json(data!) print(json) return result } getcoordinates()
here's example of output i'm getting separate json client:
{ "results": [ { "address_components": [ { "long_name": "43201", "short_name": "43201", "types": [ "postal_code" ] }, { "long_name": "columbus", "short_name": "columbus", "types": [ "locality", "political" ] }, { "long_name": "franklin county", "short_name": "franklin county", "types": [ "administrative_area_level_2", "political" ] }, { "long_name": "ohio", "short_name": "oh", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "united states", "short_name": "us", "types": [ "country", "political" ] } ], "formatted_address": "columbus, oh 43201, usa", "geometry": { "bounds": { "northeast": { "lat": 40.011147, "lng": -82.9723898 }, "southwest": { "lat": 39.976962, "lng": -83.0250691 } }, "location": { "lat": 39.9929821, "lng": -83.00122100000002 }, "location_type": "approximate", "viewport": { "northeast": { "lat": 40.011147, "lng": -82.9723898 }, "southwest": { "lat": 39.976962, "lng": -83.0250691 } } }, "place_id": "chij9rz24rwooigr3eeul2ge4oo", "types": [ "postal_code" ] } ], "status": "ok" }
i'm trying field results.geometry.location. using freddy json parsing library able results field couldn't access geometry field. can take @ see if i'm doing wrong? swiftyjson doesn't let me parse json.
the closure passed argument in datataskwithrequest
asynchronous meaning called instantly or way down road given network conditions. better pass closure in original sendjsonrequest
method while return void
. once datataskwithresult
closure called, can invoke closure response.
in terms of code, might this:
func sendjsonrequest(connectionstring: string, httpmethod : httpmethod = httpmethod.get, jsonheaders : [string : string] = [ : ], jsonstring: string = "", completion: (data: nsdata?, error: nserror?) -> void) { … //your code var urltask = session.datataskwithrequest(request) { (optionaldata, optionalresponse, optionalerror) in nsoperationqueue.mainqueue().addoperation { if let data = optionaldata { completion(data, nil) } else if let error = optionalerror { completion(nil, error) } } } urltask.resume() } // return coordinates of given location func getcoordinates(withcompletion completion: (coordinates) -> void) { let connectionstring = "https://maps.googleapis.com/maps/api/geocode/json?address=43201" sendjsonrequest(connectionstring: connectionstring) { (optionaldata, optionalerror) in if let data = optionaldata { let json = json(data) print(json) //do conversion coordinates here let coordinates = //? completion(coordinates) } // handle errors, etc… } }
one note, arguments , variables lowercased. class names should uppercase.
Comments
Post a Comment