ios - How to retrieve JSON data from webpage -


my webpage displays constructive data in single line, let app retrieve it.

json

[{"long":"1234..45","lat":"345.12"}] 

this information in page, click here see: http://www.gogrex.com/sandbox/startloc.json

how retrieve app json? have looked thru many examples still cannot solve it.

swift 3.x

class viewcontroller: uiviewcontroller {     override func viewdidload() {         super.viewdidload()         // create url         let geturl = url(string: "http://www.gogrex.com/sandbox/startloc.json")!         // use urlsession data website         urlsession.shared.datatask(with: geturl) { (data, response, error) in             guard let data = data, error == nil else {                 print(error?.localizeddescription ?? "nil")                 return             }             // need serialize data using  jsonserialization jsonobject(with:options)             {                 if let jsonarray = try jsonserialization.jsonobject(with: data, options: .allowfragments) as? [any],                     let jsondict = jsonarray.first as? [string: any] {                     let latstring = jsondict["lat"] as? string                     let longstring = jsondict["long"] as? string                     print("latitude = ", latstring ?? "nil")   // 345.12                     print("longitude = ", longstring ?? "nil")  // 1234..45  ( need fix value)                     print(double(latstring ?? "nil") ?? "nil")  // optional(345.12)                     print(double(longstring ?? "nil") ?? "nil") // nil                 }             } catch let error nserror {                 print(error.localizeddescription)             }         }.resume()     } } 

swift 2.3

class viewcontroller: uiviewcontroller {     override func viewdidload() {         super.viewdidload()         let geturl = nsurl(string: "http://www.gogrex.com/sandbox/startloc.json")!         nsurlsession.sharedsession().datataskwithurl(geturl) { (data, response, error) in             guard let data = data error == nil else {                 print(error?.localizeddescription)                 return             }             {                 if let jsonarray = try nsjsonserialization.jsonobjectwithdata(data, options: .allowfragments) as? [anyobject],                     let jsondict = jsonarray.first as? [string:anyobject] {                     let latstring = jsondict["lat"] as? string ?? ""                     let longstring = jsondict["long"] as? string ?? ""                     print("latitude = ", latstring)   // 345.12                     print("longitude = ", longstring)  // 1234..45  ( need fix value)                     print(double(latstring))  // optional(345.12)                     print(double(longstring)) // nil                 }             } catch let error nserror {                 print(error.localizeddescription)             }         }.resume()     } } 

don't forget edit info.plist add web domain

enter image description here


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -